Solved

how to skip lines in C++ when reading from a file?

Posted on 2002-03-21
14
507 Views
Last Modified: 2012-08-13
I have a data file which looks like:

#&comments lines ....
#&

1.3245 5.2341
2.3344 6.1123

#& some other comments ....

2.3001 4.5003

How to write a function to read the pairs of x and y data but skip all the comments lines? Using C++.
0
Comment
Question by:oatnusigma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +4
14 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6886913
#include <iostream>

// ...

istream str ( "myfile.txt", ios::nocreate);
char buf [ MAX_LENGTH];

 while ( !str.eof()) {

   str.getline ( buf, sizeof ( buf));

   // skip comment lines
   if ( !strncmp ( buf, "#&", 2)) continue;

   // process line
 }

 str.close();


0
 

Expert Comment

by:lazypinkmonkey
ID: 6888539
istreaf FILE("file.txt", ios::open);
char BUF[120];

FILE.getline(BUF, 120);

while(FILE)
{
  if( !isdigit(BUF[0] )   // returns true if a digit
  {                       // if not a digit, get next line
     FILE.getline(BUF, 120);
  }

  else                    // otherwise it is a digit and
  {                       // do whatever
.
.
.
  }
0
 
LVL 86

Expert Comment

by:jkr
ID: 6888552
May I ask what makes your proposal soooo unique (and different from my comment) that you just *had* to post it as an answer?

BTW, what's 'istreaf'? Can't find that class anywhere...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:lazypinkmonkey
ID: 6888608
Sorry to step on your vast expertise toes. Your comment wasn't accepted as the answer so I tried for a different approach.

Never ever made a typo in your life, eh? Well gosh golly gee, I've just found Mr. Perfect!
0
 
LVL 86

Expert Comment

by:jkr
ID: 6888745
>>Never ever made a typo in your life, eh? Well gosh golly gee, I've just found Mr. Perfect!

No, but if you want to post an "Answer", and there is already a similar comment, you'd better make sure that there are none of them :o)
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6922610
hi lazypinkmonkey,
The experts in this section have agreed to post comments rather than answers.  This is mainly to benefit the question askers since it provides more, and more varied input for them.  That convention provides the added benefit that, when followed, one expert won't ever lock a question with an answer that is substantially similar to a previously-posted comment.

So, in the future, please post comments, like everyone else.  Thanks!

-- Dan

0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6924547
Another suggestion:

ifstream f("thefile");
while (f)
{
     double x, y;
     if (!(f >> x))
     {
          f.clear();
          f.ignore(numeric_limits<int>::max(), '\n');
     }
     else
     {
          f >> y;
          // ... use x, y ...
     }    
}

or, if you feeling particularly jaunty:
ifstream f("thefile");
while (f)
{
     double x, y;
     if (!(f >> x >> y))
     {
          f.clear();
          f.ignore(numeric_limits<int>::max(), '\n');
          continue;
     }
     // ... use x, y ...
}

0
 

Expert Comment

by:campione
ID: 6929783
Its very easy to code this one, but you'll have to follow a pattern for all your programming problems. I recommend solving each problem in a logical way.

In this case

PROBLEM
-------
1. Take Input from File (not a big deal)
2. Neglect remarks (big deal lol no damn easy)
3. Take necessary input using inbuilt features of c++ which actually help out task (you'll see how)


STEPS
-----
1. I agree with most of you all on this one, just include iostream and declare ifstream object like this

ifstream("filename.txt");


2. well now our file is ready for input, so you need to neglect the remarks. My logic on this one is to take input from the file line by line and if the first two characters are '#&' then just continue loop and take no input. code for this would be

if(strncmp(line, "#$", 2)==0)
continue;
else { take input }

the strncmp function here compares the first two characters in the variable line with "#$" and if found returns 0 else returns non zero.


3. congrats you have solved almost 66.67% approx of your problem. Now our next speed breaker comes when you have to take the xy inputs
thats easy, in the else code u see above in step 2 do this

if(strncmp(line, "#$", 2)==0)
continue;
else { cin>>x>>y; } // assuming x and y are of float type.


now I'll do what others do, give a code for your problem

code snippet here
//////////////////////////////////////////////

float x[10], y[10]; // you can change this 10 hehe
ifstream DATA("filename.txt");
char buffer[100];


while(FILE) // or while(!FILE.eof())
{
  DATA.getline(buffer,100);

  if(strcmp(buffer, "#$", 2)==0)
    continue; // do nothing
  else
  {
    cin>>x[i]>>y[i];
    i++;
  }
}
//////////////////////////////////////////////

well you are done boy, but really this was a googly..

thats all folks!
0
 

Expert Comment

by:campione
ID: 6929791
aaaaww correction

////////////////////////////////////////////////////////

STEPS
-----
1. I agree with most of you all on this one, just include iostream and declare ifstream object like
this

--------> ifstream("filename.txt"); <----------

//////////////////////////////////////////////////////

thats pointed line should be ifstream DATA("filename.txt")
or any other appropriate identifier.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6930630
You might also want to discard blank lines.

And campione's line:
      if(strcmp(buffer, "#$", 2)==0)
needs a little work.  The proper function is strncmp (with an n).  Thus...
      if( (! *buffer) || strncmp(buffer, "#$", 2)==0 ) {
            continue;
      }

-- Dan
0
 

Expert Comment

by:campione
ID: 6930885
thanks Dan
0
 

Expert Comment

by:campione
ID: 6930905
thanks Dan
0
 
LVL 11

Expert Comment

by:griessh
ID: 7025081
Dear oatnusigma

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "jkr"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7042485
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question