C# 0 The Complete Reference


PART I C h a p t e r   1 4



Download 4,07 Mb.
Pdf ko'rish
bet490/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   486   487   488   489   490   491   492   493   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   1 4 :  

U s i n g   I / O  

379


PART IPART I

    Console.WriteLine("Enter text ('stop' to quit).");

    do {

      Console.Write(": ");

      str = Console.ReadLine();

      if(str != "stop") {

        str = str + "\r\n"; // add newline

        try {

          fstr_out.Write(str);

          } catch(IOException exc) {

             Console.WriteLine("Error Writing File");

             Console.WriteLine(exc.Message);

             break;

          }

      }

    } while(str != "stop");

    fstr_out.Close();

  }


}

In some cases, it might be more convenient to open a file directly using 



StreamWriter

.

To do so, use one of these constructors:



StreamWriter(string 

fi lename

)

StreamWriter(string 



fi lename

, bool 


appendFlag

)

Here, 



filename

 specifies the name of the file to open, which can include a full path specifier. 

In the second form, if 

appendFlag

 is true, then output is appended to the end of an existing 

file. Otherwise, output overwrites the specified file. In both cases, if the file does not exist, 

it is created. Also, both throw an 



IOException

 if an I/O error occurs. Other exceptions are 

also possible.

Here is the key-to-disk program rewritten so that it uses 



StreamWriter

 to open the 

output file:

// Open a file using StreamWriter.

using System;

using System.IO;

class KtoD {

  static void Main() {

    string str;

    StreamWriter fstr_out;

    try {

      fstr_out = new StreamWriter("test.txt");

    }

    catch(IOException exc) {



      Console.WriteLine("Cannot Open File");

      Console.WriteLine(exc.Message);

      return ;

    }


    Console.WriteLine("Enter text ('stop' to quit).");

www.freepdf-books.com




380

 

P a r t   I :  



T h e   C #   L a n g u a g e

    do {


      Console.Write(": ");

      str = Console.ReadLine();

      if(str != "stop") {

        str = str + "\r\n"; // add newline

        try {

          fstr_out.Write(str);

        } catch(IOException exc) {

          Console.WriteLine("Error Writing File");

          Console.WriteLine(exc.Message);

          break;

        }

      }


    } while(str != "stop");

    fstr_out.Close();

  }

}


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   486   487   488   489   490   491   492   493   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish