fscanf

This is a discussion on fscanf within the C Programming forums, part of the General Programming Boards category; say i got the following code: Code: FILE *fptr1; fptr1 = fopen ( “mydata”, “r”); fscanf(fptr1, “%d %d”, &a, &b); ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    Question fscanf

    say i got the following code:
    Code:
    FILE *fptr1;
    fptr1 = fopen ( “mydata”, “r”);
    fscanf(fptr1, “%d %d”, &a, &b);
    How does the fscanf work? does it read in words(ie characters separated by a space) and after it has a word it assigns it to the variables?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Sorta. By space you should really think whitespace. And its no miracle worker, it tries to do as you instructed it to, it can fail in the process...

    I.e. Your input file could look like this:

    file.txt
    Code:
    123456 string
    While your fscanf() tries this:

    Code:
    fscanf(fptr1, "%d %d", &a, &b);
    And returns a value of 1, since it could not parse both as you reqested.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    In this case it reads two ints from the file mydata and puts them in memory locations labeled a and b.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by itCbitC View Post
    In this case it reads two ints from the file mydata and puts them in memory locations labeled a and b.
    Or at least it tries anyway... sometimes the input may not be properly formatted.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by taurus View Post
    say i got the following code:
    Code:
    FILE *fptr1;
    fptr1 = fopen ( “mydata”, “r”);
    fscanf(fptr1, “%d %d”, &a, &b);
    How does the fscanf work? does it read in words(ie characters separated by a space) and after it has a word it assigns it to the variables?
    I this case, it would read the first whitespace delimited string, convert it to an int, and place in variable a. Then, it would do the same for variable b. fscanf() will return the number of successful reads and conversions, in this case, it should be 2. See here: http://www.cppreference.com/wiki/c/io/fscanf
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

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 causing a crash
    By dougwilliams in forum C Programming
    Replies: 6
    Last Post: 11-18-2007, 03:52 PM
  4. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 06:24 AM
  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