How do use multiple terminals in a getline()?

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
}
Last edited on
Thanks to everyone for helping -.-

Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
}
Last edited on
It looks like you're using std::string. Then it's rather easy:
1
2
3
4
5
6
std::string text;
std::string word;
while(infile >> word)
{
  text += word;
}
foo, I thought that you have spanned multiple pts.
Topic archived. No new replies allowed.