472,145 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

skipping line while reading data file

I would like know how I can skip a line while reading
a set of input data (from a text file) if the first character
of the line is "#".

My original code reads:

ifstream Infile("data.dat");

for( int i=0; i<N; i++){
Infile >x[i] >y[i];
}

Sep 4 '06 #1
3 5397
In article <11**********************@74g2000cwt.googlegroups. com>,
da*******@gmail.com says...
I would like know how I can skip a line while reading
a set of input data (from a text file) if the first character
of the line is "#".

My original code reads:

ifstream Infile("data.dat");

for( int i=0; i<N; i++){
Infile >x[i] >y[i];
}
One obvious possibility would be something like this:

std::istream &my_getline(std::istream &is, std::string &s) {
while (std::getline(is, s) && s[0] == '#')
;
return is;
}

for (int i=0; i<N; i++) {
std::string line;
if (!my_getline(Infile, line))
break;
std::stringstream temp(line);
temp >x[i] >y[i];
}

That does raise one minor question: you're starting with a count (N) of
lines to read. Is that count supposed to include the commented lines or
not? The code above attempts to read N lines of actual data -- if you
want a total of N lines, whether they contain data or not, you'd have to
rewrite it a bit.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 4 '06 #2
Thanks. I have a further question..
Jerry Coffin wrote:
One obvious possibility would be something like this:

std::istream &my_getline(std::istream &is, std::string &s) {
while (std::getline(is, s) && s[0] == '#')
;
return is;
}

for (int i=0; i<N; i++) {
std::string line;
if (!my_getline(Infile, line))
break;
Is the if-statement for checking if the Infile does exit?
std::stringstream temp(line);
temp >x[i] >y[i];
}
Sep 5 '06 #3
In article <11**********************@h48g2000cwc.googlegroups .com>,
da*******@gmail.com says...

[ ... ]
for (int i=0; i<N; i++) {
std::string line;
if (!my_getline(Infile, line))
break;

Is the if-statement for checking if the Infile does exit?
It's mostly to check whether we've reached the end of the file (for
example, if the file got corrupted and only contained one line).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by JackM | last post: by
1 post views Thread by Rick | last post: by
9 posts views Thread by Adi | last post: by
6 posts views Thread by lisa.engblom | last post: by
7 posts views Thread by Gustaf | last post: by
reply views Thread by Jerry Coffin | last post: by
reply views Thread by leo001 | last post: by

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.