C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet500/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   496   497   498   499   500   501   502   503   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

EndOfStreamException

 when the 

end of the stream is reached, the program simply reads the file until either it finds the 

desired item or this exception is generated. Thus, no special mechanism is needed to detect 

the end of the file.

Random Access Files

Up to this point, we have been using 



sequential files,

 which are files that are accessed in a 

strictly linear fashion, one byte after another. However, you can also access the contents of a 

file in random order. One way to do this is to use the 



Seek( )

 method defined by 



FileStream

.

This method allows you to set the 



file position indicator

 (also called the 



file pointer

 or simply 

the

current position

) to any point within a file.

The method 

Seek( )

 is shown here:

long Seek(long 

newPos

, SeekOrigin 



origin

)

Here, 



newPos

 specifies the new position, in bytes, of the file pointer from the location specified 

by

origin.

 The origin will be one of these values, which are defined by the 



SeekOrigin

enumeration:

Value

Meaning


SeekOrigin.Begin

Seek from the beginning of the file.

SeekOrigin.Current

Seek from the current location.

SeekOrigin.End

Seek from the end of the file.

After a call to 

Seek( )

, the next read or write operation will occur at the new file position. 

The new position is returned. If an error occurs while seeking, an 

IOException

 is thrown. 

If the underlying stream does not support position requests, a 

NotSupportedException

 is 


thrown. Other exceptions are possible.

Here is an example that demonstrates random access I/O. It writes the uppercase 

alphabet to a file and then reads it back in nonsequential order.

// Demonstrate random access.

using System;

using System.IO;

class RandomAccessDemo {

  static void Main() {

    FileStream f;

    char ch;

    try {

      f = new FileStream("random.dat", FileMode.Create);

    }

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   496   497   498   499   500   501   502   503   ...   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