skip blank lines

P

puzzlecracker

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
 
D

Dave Townsend

puzzlecracker said:
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") )
{
}
 
K

Kai-Uwe Bux

Dave said:
puzzlecracker said:
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
 
J

Jim Langston

puzzlecracker said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top