fscanf

This is a discussion on fscanf within the C Programming forums, part of the General Programming Boards category; when you say scan, i think it scans through the file, but it doesn't appear that way. if i open ...

  1. #1
    Banned
    Join Date
    Aug 2004
    Posts
    128

    fscanf

    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
    Ex.
    Code:
    int myInt;
    fscanf(stream,"%ld",&myInt);
    does not work, unless a number is the first thing
    in the file.

    Ex
    Code:
    12 asdifj
    if the following lines are in the file, no valid number
    is returned :
    Code:
    adsfkl37adfjkasd
    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
    int's?

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > fscanf
    Means
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.
    Code:
    /* file.txt
    adsfkl37adfjkasd
    asdiufhaiueis 87
    asdi+uf-2ha+33iu
    */
    
    #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 )
             {
                break;
             }
             if ( isdigit(ch) || ch == '+' || ch == '-' )
             {
                if ( ungetc(ch, file) != EOF )
                {
                   int number;
                   if ( fscanf(file, "%d", &number) == 1 )
                   {
                      printf("number = %d\n", number);
                   }
                }
             }
          }
          fclose(file);
       }
       else
       {
          perror(filename);
       }
       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

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