fgets and skipping a line...

This is a discussion on fgets and skipping a line... within the C Programming forums, part of the General Programming Boards category; Hi, I'm using fgets to read in from a file... What I want to do is have it check to ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    83

    fgets and skipping a line...

    Hi, I'm using fgets to read in from a file...
    What I want to do is have it check to see if the first character of a line is 'a', if so, it moves to next line and then does something with that next line... Here is what I have so far, but I'm not sure how to skip to the next line, if you see what I mean.
    Thanks.

    Code:
        char OneLine[200];
        
        FILE *fp = fopen("file.txt","r");
        
        if (fp != NULL) {
            while (fgets(OneLine, 199, fp) != NULL) {
    			
    			if (OneLine[0] == 'a')
    			{
                                   // SKIP TO NEXT LINE...
                                   // if next line....
    			}

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    If you do nothing else, you'll return to the while statement and read the next line automatically.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    979
    This has nothing to do with your question, but just so you know: You can pass fgets() the entire size of the array; you don't have to subtract one (unless, for some reason, you want to last byte of the array to be untouched by fgets()). This way you can do something easy like:
    Code:
    char buf[1024];
    while(fgets(buf, sizeof buf, fp) != NULL) { ... }

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Cool, thanks to both of you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashing at fgets() - any ideas why?
    By avi2886 in forum C Programming
    Replies: 4
    Last Post: 03-06-2009, 08:24 PM
  2. Skipping the line.
    By Volair in forum C Programming
    Replies: 12
    Last Post: 11-20-2006, 03:35 PM
  3. fgets error handling
    By mlsbbe in forum C Programming
    Replies: 5
    Last Post: 03-24-2003, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21