473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 26313

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
5
by: Tappy Tibbons | last post by:
I have a class I am serializing, and need the resultant XML to skip/omit classes that are not initialized, or their member variables have not been set. Is this possible? Say for the following...
6
by: Melissa | last post by:
Does anyone have a generic procedure for adding blank lines to reports like Sales details, PO details and/or Orders details. The procedure would need to count the number of line items, determine...
12
by: FrancisC | last post by:
How can I open a file, skip first 2 lines and get the 50th character? EXP 0 R:\000\un\111\e00\e00noLog\1006\bdry_arc.e00 ARC 2 1 1 0 0 0 0 7 ...
4
by: Just Me | last post by:
In the handler QueryPageSettings I let the user select a "Skip This Page" option. But I don't know how to handle it if he does. I can set a flag and in PrintPage skip printing the page but the...
3
by: Jeff Calico | last post by:
Hello everyone I am transforming an XML document to text, basically only outputting a small portion of it. When I run the following XSLT via Xalan's processor, I get a bunch of unwanted blank...
4
by: John | last post by:
I need to read data from the file like the following with name and score, but some line may only has name without score: joe 100 amy 80 may Here's my code, but it couldn't read the line with...
2
by: jatindersingh80 | last post by:
Hi , i am reading a text file with token # but somtimes blank lines comes between the line at blank line the code gives exception of no such element the text files contains data after blank lines...
18
by: luckyyyyyy | last post by:
I appriate your work but... guys i have another problm.....i wana skip first 9 lines from my data file and after skipping i wana read x,y,z at the end of file. i did but after while loop is end it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.