fscanf() and fgetc()

This is a discussion on fscanf() and fgetc() within the C Programming forums, part of the General Programming Boards category; Let's say I have a file with the following characters i suck at this and I use fscanf() to access ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    31

    fscanf() and fgetc()

    Let's say I have a file with the following characters

    i suck at this

    and I use fscanf() to access the text file.
    After accessing 'i' , will the file pointer skip over i to the white space? Or will it continue to pt at i?

    In simple terms, is there any difference in the behaviour of the file pointer after using fscanf() and fgetc() to access a text file?

    Pier.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    fgetc() will read only one character, whereas fscanf() will read whatever you have told it to, and more if you're not careful. The internal file pointer will end up being moved relative to the number of bytes reads.

    Depending on what you're doing, fgets() may be the way to go.

    Unless you're confident the input data is always going to be well structured, I'd suggest avoiding fscanf().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    Originally posted by Hammer

    Depending on what you're doing, fgets() may be the way to go.

    Unless you're confident the input data is always going to be well structured, I'd suggest avoiding fscanf().
    You're suggesting that I read each line using fgets and then process it internally using sscanf ?

    Pier.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by MadStrum!
    You're suggesting that I read each line using fgets and then process it internally using sscanf ?
    I'm suggesting using fgets() to grab the input line. This way you can limit the number of characters read in, and you won't overflow an array buffer (assuming you do it correctly).

    How you do process the buffer depends on what you want to do with it.

    But if you want the quick solution, yes, you can use sscanf(). Here's a sample to get you going.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char buf[BUFSIZ];
      char word[BUFSIZ];
      char *p = buf;
      int wordlen;
      
      if (fgets(buf, BUFSIZ, stdin) != NULL)
      {
        while (sscanf(p, "%30s%n", word, &wordlen) == 1)
        {
          printf ("Word %d >%s<\n", wordlen, word);
          p += wordlen;
        }
      }
      return 0;
    }
    I suppose you could use fscanf() instead (as you suggested). It all comes down to what you want to do with the data and how much you trust it to be formatted correctly.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    Ok! Another simple question....
    How do I detect it when fgets reads in a blank line?

    Pier.

  6. #6
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    a blank line is only a \n sign so just

    compare the string inputted by fgets with "\n" ....

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    Ok! Damm I must have lost a memory module or something...

    Pier.

Popular pages Recent additions subscribe to a feed

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