473,382 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to read large text file?

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
Aug 15 '06 #1
6 4008
Thank you, Vadym
It works great.

"Vadym Stetsyak" wrote:
Hello, elena!

eHi, All
eI have text file ASCII with record length of 388 bytes, no record
edelimeter and size of the file is 562477780 bytes and 1449685 records
eall togeter. How can i read such file record by record ?

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

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot
Aug 15 '06 #2
Vadym Stetsyak <va*****@ukr.netwrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 15 '06 #3
Jon Skeet [C# MVP] <sk***@pobox.comwrote:
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
Doh - wrong URL.
http://www.pobox.com/~skeet/csharp/readbinary.html
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 15 '06 #4
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
Aug 16 '06 #5
Vadym Stetsyak wrote:
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

Aug 16 '06 #6
Hello, Jon!

JSCI think you've missed my point. Just because you call Read with a
JSCbuffer of 388 bytes, even if there are more than 388 bytes there's
JSCnothing to guarantee that all 388 bytes will be filled in a single
JSCcall.

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
Aug 16 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chuck Amadi | last post by:
Has anyone got a simple python script that will parse a linux mbox and create a large file to view . Cheers Chu
7
by: CJ | last post by:
Hello! I want to read a large text file (100 mb) and from with in the file i want to take out some lines on a spesific kriteria and make new files. eks. inputfile: 123 hello.. 123 more......
9
by: sweety | last post by:
Dear All, How to encrypt a C data file and make binary file and then have to read a bin file at run time and decrypt the file and have to read the data. Any help to achive this pls. Would be...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
2
by: Jarry | last post by:
I am a bit new to VB, and I have set up a program which loads a large text file. The text file (.txt) has a file size of around 6 megabytes. This is the figure I would like to rreduce. I have...
2
by: starffly | last post by:
I want to read a xml file in Unicode, UTF-8 or a native encoding into a wchar_t type string, so i write a routine as follows, however, sometimes a Unicode file including Chinese character cannot...
3
by: utab | last post by:
Dear all, What are the advantages of binary files over text files? I would like to search for a specific value of a variable in an output file, I was doing this lately by the string library...
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.