C# 0 The Complete Reference


PART I C h a p t e r   1 4



Download 4,07 Mb.
Pdf ko'rish
bet492/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   488   489   490   491   492   493   494   495   ...   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  

381


PART IPART I

    try {

      while((s = fstr_in.ReadLine()) != null) {

        Console.WriteLine(s);

      }

    } catch(IOException exc) {

      Console.WriteLine("Error Reading File");

      Console.WriteLine(exc.Message);

    }

    fstr_in.Close();



  }

}

In the program, notice how the end of the file is determined. When the reference returned 



by

ReadLine( )

 is null, the end of the file has been reached. Although this approach works, 



StreamReader

 provides an alternative means of detecting the end of the stream: the 



EndOfStream

 property. This read-only property is true when the end of the stream has been 

reached and false otherwise. Therefore, you can use 

EndOfStream

 to watch for the end of a 

file. For example, here is another way to write the 

while

 loop that reads the file:

while(!fstr_in.EndOfStream) {

  s = fstr_in.ReadLine();

  Console.WriteLine(s);

}

In this case, the use of 



EndOfStream

 makes the code a bit easier to understand but does 

not change the overall structure of the sequence. There are times, however, when the use 

of

EndOfStream

 can simplify an otherwise tricky situation, adding clarity and improving 

structure.

As with 

StreamWriter

, in some cases, you might find it easier to open a file directly 

using

StreamReader

. To do so, use this constructor:

StreamReader(string 

fi lename

)

Here, 



filename

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

The file must exist. If it doesn’t, a 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   488   489   490   491   492   493   494   495   ...   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