Thread: fscanf

  1. #1


    when you say scan, i think it scans through the
    file, but it doesn't appear that way.

    if i open a file (FILE *), and then try to scan a int
    int myInt;
    does not work, unless a number is the first thing
    in the file.

    12 asdifj
    if the following lines are in the file, no valid number
    is returned :
    asdiufhaiueis 87
    My question is why doesn't it work, and is there a way to
    scan from the beginning of the file to the end and pull out all

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Don't make assumptions based on the functions name. Its pretty much an input version of printf. To make a function that "scans" in the sense you are thinking of, you'd have to read in every line from the file (whether all in one go, or read as you process) and just search each line for valid numbers, and pull them all out into another matrix or another file.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > fscanf
    File SCAN Formatted.

    Means you need to agree on the format of the text file so that the producer of the text file and the consumer (your program) can work together.
    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.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Quote Originally Posted by JarJarBinks
    is there a way to scan from the beginning of the file to the end and pull out all int's?
    Maybe something like this.
    /* file.txt
    asdiufhaiueis 87
    #include <stdio.h>
    #include <ctype.h>
    int main(void)
       const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file )
          for ( ;; )
             int ch = getc(file);
             if ( ch == EOF )
             if ( isdigit(ch) || ch == '+' || ch == '-' )
                if ( ungetc(ch, file) != EOF )
                   int number;
                   if ( fscanf(file, "%d", &number) == 1 )
                      printf("number = %d\n", number);
       return 0;
    /* my output
    number = 37
    number = 87
    number = -2
    number = 33
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. fscanf help
    By Axel in forum C Programming
    Replies: 13
    Last Post: 10-22-2006, 10:44 PM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 01:32 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM
Website Security Test