469,568 Members | 1,620 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

skip blank lines



I want to read lines and skip blank lines:

would this work considering the lines can contain tabs, spaces, etc.?
file.in:
------
line1

line2

line3
...

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main(int argc, char *argv ){

read(argv[1]);
}

void read(char * filename){

infile in(filename);
string line;
while(getline(in, line)&&line!="")
//do something

in.close();

}

well just look at read..

thanks

Oct 16 '05 #1
3 25204

"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...


I want to read lines and skip blank lines:

would this work considering the lines can contain tabs, spaces, etc.?
file.in:
------
line1

line2

line3
..

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main(int argc, char *argv ){

read(argv[1]);
}

void read(char * filename){

infile in(filename);
string line;
while(getline(in, line)&&line!="")
//do something

in.close();

}

well just look at read..

thanks

Your code doesnt' skip blank lines, just empty lines. You need something
like this
to scan the entire line to see that there's some non-whitespace..

if ( line.find_first_not_of(" \t\v\r\n") )
{
}
Oct 17 '05 #2
Dave Townsend wrote:

"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...


I want to read lines and skip blank lines:

would this work considering the lines can contain tabs, spaces, etc.?
file.in:
------
line1

line2

line3
..

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main(int argc, char *argv ){

read(argv[1]);
}

void read(char * filename){

infile in(filename);
string line;
while(getline(in, line)&&line!="")
//do something

in.close();

}

well just look at read..

thanks

Your code doesnt' skip blank lines, just empty lines.

[snip]

It does not skip empty lines either. It simply stops when it sees the first
blank line or the end of the file.
Best

Kai-Uwe Bux
Oct 17 '05 #3

"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...


I want to read lines and skip blank lines:

would this work considering the lines can contain tabs, spaces, etc.?
file.in:
------
line1

line2

line3
..

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main(int argc, char *argv ){

read(argv[1]);
}

void read(char * filename){

infile in(filename);
string line;
while(getline(in, line)&&line!="")
//do something


First, I think you want:
while ( getline( in, line ) )
if ( line != "" )
// do something

so it'll continue to read even if it sees an empty line.

Read the other response, becuase if the line has a single space, it won't
work.
Oct 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Melissa | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.