Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting file line

Status
Not open for further replies.

timmay3141

Programmer
Dec 3, 2002
468
US
I haven't used the fstream class for file input/output for a while, and I was wondering how you go to a certain line in the file. I'm going to have some data in a file in this format: int double string. The file could look like this:

1 2.05 Hello
8 4.5 Bye
6 2.1323 asdf
etc

I thought there was a function to go to a specific line, but I don't remember. Anyone know this function, or does it not exist? I'm using this in a win32 app, so if there is a more "Windows" way of handling file input/output that can do the same stuff, could you point me in the right direction?
 
I would have a look at CStdioFile. The ReadString function. I haven't used it myself but I ususally use CFile and read char by char until the end-of-line marker.

Hope this is helpful

MJ
 
>> and read char by char

Ok, that will work… I guess… but is extremely inefficient. My advice is to NEVER read a file one byte at a time. It’s a simple enough thing to write a buffered approach and a single byte approach and time them. Actually even different size buffers effect the speed dramatically.


-pete
 
Palbano, can you clearify exactly what you mean? Do you mean use the getline function repeatedly or what?
 
I'd prolly do it like this:

Read the entire file in one go into a string (for example CString if applicable) and then extract the lines/whatever from the string.



Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
PerFnurt
Ok, reading into a single buffer can work if you want to allocate massive amounts of memory for reading files, or if you 100% sure the files are small. While that approach is faster than reading single bytes, it is still not as fast as matching the IO buffer size within your application. As i stated above you can perform very simple tests to prove this to yourself.

timmay3141
No I’m not talking about readline. You create like a 4k buffer and use it repeatedly to read the contents of the file. Then for each buffer read you scan the contents of the memory buffer to locate the data you are interested in.

Depending on your requirements there are many minor variations of doing this. For instance if you have random length records in the file, you might use a 8k buffer as two different 4k buffers. This way you will always have the previous one in memory. They can be used to generate a complete record when the reads split a record across the two buffers.

Another variation is as PurFnurt suggests, read the entire file into a single buffer but do it 4k at a time for speed. Say your file is 2 meg, you allocate a 2 meg byte buffer and call read the first time pointing to the starting point of the buffer and read only 4k, Then you move the pointer forward in the buffer and read the next 4k into that location, continue until the entire 2 meg is read into the buffer.




-pete
 
I see what you're saying, palbano, but why 4k? Is there some special reason that reading 4k would be faster than reading all 2MB?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top