Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > skip blank lines

Reply
Thread Tools

skip blank lines

 
 
puzzlecracker
Guest
Posts: n/a
 
      10-16-2005


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

 
Reply With Quote
 
 
 
 
Dave Townsend
Guest
Posts: n/a
 
      10-17-2005

"puzzlecracker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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") )
{
}


 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-17-2005
Dave Townsend wrote:

>
> "puzzlecracker" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      10-17-2005

"puzzlecracker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
skip first N lines when reading file Jie Perl Misc 6 06-28-2007 09:07 AM
Preserve blank lines when add multiple lines of text to a cell Cah Sableng Javascript 0 04-23-2007 04:46 AM
How do i skip n number of lines in a file based on a pattern of the record sanju.shah@gmail.com Perl Misc 5 07-08-2005 09:28 PM
How can I open a file, skip first 2 lines and get the 50th character? FrancisC C Programming 16 10-14-2003 12:34 PM
How can I open a file, skip first 2 lines and get the 50th character? FrancisC C++ 7 10-10-2003 07:11 PM



Advertisments