Archived

This topic is now archived and is closed to further replies.

AbsolutDew

trailing whitespace at end of files

Recommended Posts

Hey, im reading in lines from a file one by one into an array, and was wondering if there was anyway to truncate whitespace at ends of files. So if there was a buncha hard returns at the end of a file, it wouldnt read blank lines into my array before eof. Currently im just checking if line is blank and breaking from the loop, but what happens if i want an occasional blank line read in, just not 3-4 accidental ones at the end of a file. It''s probably obvious but im not very good with file io, and google wasn''t much help. Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try this:

Read the entire file, line by line, into a list (such as std::list<LINE> as you are already doing. The the loop condition should be something like while( !myfile.eof() ) or while( !feof(myFile) ), depending on whether you''re using C or C++ file io.

When the loop ends, enter a second loop that starts at the end of the list and works its way back to the front of the list. Check to see if the current LINE contains only whitespace. If it does, delete it from the list. If not, end the loop.

Note, LINE is just a placeholder name for whatever you''re using to hold the file''s data.

Share this post


Link to post
Share on other sites
Have your parsing engine use returns as delimiters, yet don''t begin parsing again until you find a viable character. This way, the parser would "skip" all the hard returns at the end of the file, yet since it would reach eof before another character (because there are none), it would work. But it all depends on what code you use the read the file. Unless you are working with your own general purpose parser, then just use the other suggestion

Share this post


Link to post
Share on other sites