I am reading a file of text. I want to read in every word, but no spaces or newlines. "word" is a string, and "c" is a char (used for getting rid of white space.
The problem: I can get rid off spaces perfectly, but newlines remain in "word" if it comes before the terminating character ' '.
My code:
1 2 3 4 5 6 7 8 9 10 11
while(infile.good() && !infile.eof())
{
while(infile.peek() == ' ')
infile >> c;
while(infile.peek() == '\n')
infile.ignore(10, '\n');
getline(infile, word, ' ');
//this line reads in the '\n' character if there is one and stores it in the back of the string
}
while(infile.good() && !infile.eof())
{
n = -1;
while(infile.peek() == ' ')
infile >> c;
while(infile.peek() == '\n')
infile.ignore(10, '\n');
getline(infile, word, ' ');
//this line reads in the '\n' character if there is one and stores it in the back of the string
n = word.find('\n');
if(n != -1)
word = word.substr(0, n);
}