Thread: fgets and skipping a line...

  1. #1
    Registered User
    Join Date
    Nov 2008

    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.

        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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

  3. #3
    Registered User
    Join Date
    Sep 2007
    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:
    char buf[1024];
    while(fgets(buf, sizeof buf, fp) != NULL) { ... }

  4. #4
    Registered User
    Join Date
    Nov 2008
    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, 09:24 PM
  2. Skipping the line.
    By Volair in forum C Programming
    Replies: 12
    Last Post: 11-20-2006, 04:35 PM
  3. fgets error handling
    By mlsbbe in forum C Programming
    Replies: 5
    Last Post: 03-24-2003, 02:45 PM