944,054 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 852
  • C++ RSS
Oct 19th, 2009
0

Ignoring First Line of File I/O

Expand Post »
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 ?

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
Web_Sailor is offline Offline
164 posts
since Jun 2009
Oct 19th, 2009
-7
Re: Ignoring First Line of File I/O
You can't just simply ignore it. You have to read it and then don't use it for anything.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Oct 19th, 2009
0
Re: Ignoring First Line of File I/O
You could try this:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
Oct 19th, 2009
0
Re: Ignoring First Line of File I/O
Click to Expand / Collapse  Quote originally posted by amrith92 ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 19th, 2009
0
Re: Ignoring First Line of File I/O
Thanks guys for all your help
Reputation Points: 10
Solved Threads: 0
Junior Poster
Web_Sailor is offline Offline
164 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Visual C++ 2005 Express Problem to Locate and Download
Next Thread in C++ Forum Timeline: String input problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC