Click to See Complete Forum and Search --> : accessing a specific area in a file...?


gilly914
February 24th, 2008, 03:04 PM
How can i access a specific range of characters in file?

for example I want to open a text file and read from character #39 to character #99.

Is there a way of doing this without reading the whole file using the FileStream.ReadToEnd() Method, and then accessing the specific characters???
The ReadToEnd method could take a long time if it's a big file, and i only need a small portion of the file content...

Tischnoetentoet
February 25th, 2008, 07:38 AM
FileStream.Seek() (http://msdn2.microsoft.com/en-us/library/system.io.filestream.seek.aspx)

jon.borchardt
February 25th, 2008, 12:43 PM
Or:

//Create a file stream from an existing file.
FileInfo fi=new FileInfo("c:\\csc.txt");
FileStream fs=fi.OpenRead();

//Read 100 bytes into an array from the specified file.
int nBytes=100;
byte[] ByteArray=new byte[nBytes];
int nBytesRead=fs.Read(ByteArray, 0, nBytes);
Console.WriteLine("{0} bytes have been read from the specified file.", nBytesRead.ToString());

gilly914
February 25th, 2008, 12:47 PM
Thanks!
I'll try it, and get back to you...