How to read large text file?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, All
I have text file ASCII with record length of 388 bytes, no record delimeter
and size of the file is 562477780 bytes and 1449685 records all togeter.
How can i read such file record by record ?

Please, help
 
Vadym Stetsyak said:
You can read eather one record after another, or mutliple records at time.

for one record at time code will look like this.

byte[] record = new byte[388];
using(FileStream fileStream = new FileStream(fileName, FileMode.Open))
{
while(fileStream.Position != fileStream.Length)
{
// Read record
fileStream.Read(record, 0, record.Length);

//Display record
Console.WriteLine(Encoding.ASCII.GetString(record));
}
}

if you want to read multuple records at time just increase record array size
byte[] record = new byte[5*388]; //to read 5 records at time

Note that you should use the return value of Read - there's nothing to
guarantee that you'll get all 388 bytes in a single call.

See http://www.pobox.com/~skeet/csharp/binaryio.html
 
Hello, Jon!

Thanks for the link.

Since, OP knows the size of the file, buffer manipulations can be avoided.

About the code in the sample, wouldn't it be simpler to use MemoryStream instead
of manually manipulating with read buffer?

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Vadym said:
Thanks for the link.

Since, OP knows the size of the file, buffer manipulations can be avoided.

I think you've missed my point. Just because you call Read with a
buffer of 388 bytes, even if there are more than 388 bytes there's
nothing to guarantee that all 388 bytes will be filled in a single
call.
About the code in the sample, wouldn't it be simpler to use MemoryStream instead
of manually manipulating with read buffer?

It would be simpler - but I've tried to provide fairly efficient code
in the sample. You could indeed just copy the whole thing to a
MemoryStream and call ToArray. I'll update the page to indicate that.

Jon
 
Hello, Jon!

JSC> I think you've missed my point. Just because you call Read with a
JSC> buffer of 388 bytes, even if there are more than 388 bytes there's
JSC> nothing to guarantee that all 388 bytes will be filled in a single
JSC> call.

Nope, I didn't missed the point, I agree with you that there is no guarantee
that the reader will obtain requested amount of bytes in single call.

This fact is far more obvious when ,the stream used, is a NetworkStream.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top