Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   skip blank lines (http://www.velocityreviews.com/forums/t449093-skip-blank-lines.html)

puzzlecracker 10-16-2005 04:06 AM

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


Dave Townsend 10-17-2005 01:15 AM

Re: skip blank lines
 

"puzzlecracker" <ironsel2000@gmail.com> wrote in message
news:1129435595.095976.203280@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") )
{
}



Kai-Uwe Bux 10-17-2005 01:46 AM

Re: skip blank lines
 
Dave Townsend wrote:

>
> "puzzlecracker" <ironsel2000@gmail.com> wrote in message
> news:1129435595.095976.203280@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

Jim Langston 10-17-2005 05:33 AM

Re: skip blank lines
 

"puzzlecracker" <ironsel2000@gmail.com> wrote in message
news:1129435595.095976.203280@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.




All times are GMT. The time now is 03:18 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.