Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
It's pretty common that I will use fgets() in a loop to iterate through the lines of a file and process them. Normally it works well but in some cases it sucks.

Example 1: If you need to know information about the next line(s) or previous line(s) to decide how to process the current line.

Example 2: The current line indicates you have read too far and you must send it to a different context of your parser.

I know of many workarounds, but overall they tend to be untenable hacks.

I could do read the entire file in as an array of strings, and then crawl through it with relative ease.

I think this is the best solution but it comes with a major downside: memory consumption equivalent to the file size. As far as I know fopen() and fgets() are not doing that.

What are your thoughts on a best compromise? Create a file I/O interface with a small cache?

Thanks

What I have tried:

Storing two or three lines at a time and using references to process them as a set

Using an un-gets-line function to push lines back onto a stack if they have been read and need to be unread, drawing new lines off of the stack until it runs out and then fetching fresh ones.
Posted
Updated 13-Dec-18 21:12pm

How about create index?
In this case, you will have to read the file twice, but, you will have complete knowledge of your data set.

You will create indexes by reading each character and seeking new line, when you hit the newline register it in memory, as array or as linked list. Using this you don't even have to cache any string, just hop from index to index.
 
Share this answer
 
Comments
Rick York 14-Dec-18 3:00am    
If have used this technique but occasionally it has problems. You have to open the file in text mode to read it and ftell can occasionally have problems in text mode. Most of the time it works but it is very frustrating when it doesn't.
Mohibur Rashid 14-Dec-18 5:23am    
Ignore \r count only \n
Rick York 14-Dec-18 18:13pm    
If reading each character that can work.
When it is the work of the app to parse the complete file it should be no problem. Test it.

At best you use some standard containers like int this file reading example code.
 
Share this answer
 
Quote:
Example 1: If you need to know information about the next line(s) or previous line(s) to decide how to process the current line.

Example 2: The current line indicates you have read too far and you must send it to a different context of your parser.

Both of such problems are usually solved in parsers using the look-ahead mechanism (see for instance Parsing - Wikipedia[^] ), that, if I am not wrong, you already find yourself as a possible solution
Quote:
Storing two or three lines at a time and using references to process them as a set
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900