Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > skipping line while reading data file

Reply
Thread Tools

skipping line while reading data file

 
 
Ivan Liu
Guest
Posts: n/a
 
      09-04-2006
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];
}

 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      09-04-2006
In article <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Ivan Liu
Guest
Posts: n/a
 
      09-05-2006
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];
> }
>


 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      09-05-2006
In article <(E-Mail Removed) .com>,
(E-Mail Removed) 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.
 
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
Skipping bytes while reading a binary file? Lionel Python 2 02-06-2009 01:20 PM
Re: Skipping bytes while reading a binary file? MRAB Python 3 02-05-2009 11:51 PM
Parsing a text file line-by-line: skipping badly-formed lines? denis.papathanasiou@gmail.com Perl Misc 27 05-18-2007 07:07 PM
skipping a structure member while initializing Mik0b0 C Programming 12 04-15-2007 08:13 AM
identifying end of line while reading data from an ifstream omariqbalnaru C++ 3 11-23-2006 09:34 PM



Advertisments