ifstream check if empty

Feb 15, 2012 at 6:53pm
Is it possible to check whether a line is empty in a stream? This is the part where I like to be more picky then just taking every row:
1
2
3
4
		while ( infile.good() ) {
			getline (infile,line);
			//cout << line << endl; //test ok if infile is prepared. 
		}

The 'program' is taking a file and chops in into pieces and I would prefer to have it ignore break row empty rows etc. Just to make it easier to use, it's for a friend and he's not exactly a computer nerd.
Feb 15, 2012 at 7:05pm
Can't you you check the length of the line
if (line.length() > 0)
Feb 15, 2012 at 7:08pm
You could simply add if(line != "") ... to ignore empty lines, though that won't work if there is a space on that line.
You could add infile >> ws;//needs <iomanip> before getline to ignore any white spaces, but this will also remove spaces (up to the first non space) from lines that contain text.
If these don't work, you'll have to write your own function bool isNotJustSpaces(string str). Simply iterate through the given string and if any char is not ' ' or '\t' (tab), return true.
Feb 15, 2012 at 7:10pm
Dude, the ws stunt is even better. The string is supposed to be without white space, br and other junk. Thanks!!!
Topic archived. No new replies allowed.