I'm having alot of difficulty taking a text file and removing the blank lines from it. In Windows, the function works properly, but for some reason when I compile and run it in the Linux environment, the blank lines are still read in and counted as valid lines (as if they were not blank). I'm extremely stumped on this one.
If it treats a seemingly blank line as non-empty, you have the source code and the debugger at your disposal to see exactly what the contents of the string are.
If you simply copied the Windows text file to Linux, then I would expect the size of the string returned by getline() for the "blank" lines to be 1 and contain '\r', but do take a look on your own.
You should not loop on eof(), it is better to do something like this:
1 2 3 4 5 6 7 8 9 10 11 12
while(getline(fin, read))
{
// now we know the read was successful
// if (read.length() > 0)
if (!read.empty()) // potentially more efficient
{
fout << read << endl;
}
else
ignore = read;
}
Also as Cubbi says the line may contain invisible spaces. You can trim the whitespace from each end of the lines using something like this: