473,804 Members | 3,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4026
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(FileStrea m fileStream = new FileStream(file Name, FileMode.Open))
{
while(fileStrea m.Position != fileStream.Leng th)
{
// Read record
fileStream.Read (record, 0, record.Length);

//Display record
Console.WriteLi ne(Encoding.ASC II.GetString(re cord));
}
}

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.ne twrote:
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(FileStrea m fileStream = new FileStream(file Name, FileMode.Open))
{
while(fileStrea m.Position != fileStream.Leng th)
{
// Read record
fileStream.Read (record, 0, record.Length);

//Display record
Console.WriteLi ne(Encoding.ASC II.GetString(re cord));
}
}

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.co m>
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.co mwrote:
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.co m>
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
8864
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
13732
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... 124 text.. 125 text
9
5166
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 great if any sample source code provided. Thanks, Sweety
35
11526
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", "r+"); fseek(file, -2, SEEK_END); fscanf(file, "%d", &c); this works fine if the integer is only a single character. When I get into larger numbers though (e.g. 502) it only reads in the 2. Is there
2
1114
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 limited space on a server, and this is a considerable chunk. anyhow, is there a file extension which would reduce the .txt file size down, but that could still be read by a stream reader. Thanks, Jarry
2
7542
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 be read completely. and I cannot tell where its root located, so NEED your help, GIVE me a hand please. THX. static Status LoadXMLFile2String(const char *filename, wchar_t *text){ FILE *f; if(!(f = fopen(filename, "r"))){ __printDebugA("Input...
3
18461
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 functions of the language. But wondering the advantages of searching and reading from a binary file. If the files get too large, I guess the advantage of reading from a binary file is that it is much faster. Regards,
3
2963
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 datatype for the file instead of using IMAGE datatype. I can not use SqlDataReader to read the data. I need your help, Thanks. -David (1) I have a table TestFile for testing: ID int FileName navrchar(255)
2
5460
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 is several GB in size. When it comes time to read the 5+GB file from inside the zip file, it fails with the following error: File "...\zipfile.py", line 491, in read bytes = self.fp.read(zinfo.compress_size) OverflowError: long it too large to...
4
3420
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 database. What I need help with is on how to locate a specific line in the file, change it and then save the updated text file. Can anyone help me out or point me to a site that explains this clearly? Here's part of my code that reads the contents of...
0
9707
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.