Ignoring First Line of File I/O

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 130
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster

Ignoring First Line of File I/O

 
0
  #1
Oct 19th, 2009
Hi.. How do I skip the first line of a text file. I am not using getline() so I think I cannot use ignore() function. Is there any other way to ignore first line ?

  1.  
  2. .................
  3. FILE *in;
  4. char line[1000];
  5. char *token;
  6. in = fopen(argv[1],"rt+");
  7. if( in == NULL) exit(1);
  8. int i=0;
  9. while(! feof(in)) {
  10.  
  11. //in.ignore(1000, '\n');
  12. // std::in.ignore ( 1000, '\n' );
  13. fgets(line, 1000, in);
  14. .................................

Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,388
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning
 
-7
  #2
Oct 19th, 2009
You can't just simply ignore it. You have to read it and then don't use it for anything.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 139
Reputation: amrith92 is on a distinguished road 
Solved Threads: 14
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster
 
0
  #3
Oct 19th, 2009
You could try this:
  1. FILE *in;
  2. char line[1000];
  3. char *token;
  4. in = fopen(argv[1],"rt+");
  5. if ( in == NULL) exit(1);
  6. int i=0;
  7. // Read the first line... move the file pointer to
  8. // point to the end of the first line. Note that the
  9. // definition for end-of-line is the '\n' character.
  10. fgets(line, 1000, in);
  11. while (! feof(in))
  12. {
  13.  
  14. // in.ignore(1000, '\n');
  15. // std::in.ignore ( 1000, '\n' );
  16. fgets(line, 1000, in);
  17. cerr << line << endl;
  18. }

Hope this helped!

Edit: I mean the same thing as what Ancient Dragon said in his post... He answered as I was typing this...
Last edited by amrith92; Oct 19th, 2009 at 3:06 pm.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #4
Oct 19th, 2009
Originally Posted by amrith92 View Post
You could try this:
    fgets(line, 1000, in);
    while (! feof(in))
    {
        // ...
    }
Why it's bad to use feof() to control a loop

[edit]I think the "rt+" mode is a nonstandard extension meaning "r+" .

[edit]And it might be preferable to do
fgets(line, sizeof line, in);
instead of
fgets(line, 1000, in);
(Preferred by me at least. )
Last edited by Dave Sinkula; Oct 19th, 2009 at 3:43 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 130
Reputation: Web_Sailor is an unknown quantity at this point 
Solved Threads: 0
Web_Sailor's Avatar
Web_Sailor Web_Sailor is offline Offline
Junior Poster
 
0
  #5
Oct 19th, 2009
Thanks guys for all your help
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC