fscanf not working

This is a discussion on fscanf not working within the C Programming forums, part of the General Programming Boards category; trying to simply scan a number from a text file "bigint.txt" and print it out to screen, but program keeps ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    fscanf not working

    trying to simply scan a number from a text file "bigint.txt" and print it out to screen, but program keeps crashing.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        //n will represent the number of operations to carry out, read in from file
        int n=0;
        FILE* fin;
       
        fin = fopen("bigint.txt", "r");
           
        fscanf(fin, "%d ", &n);
        printf("%d\n", n);
    
        fclose(fin);
        
        system("PAUSE");    
        return 0;
    }
    any ideas?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What if your fopen fails?

    What if your fscanf fails (i.e., it does not contain data formatted as you expect)?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Check whether your file contains the same order that you mention in the fscanf.That means your file should begin with the integer.

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Or whitespace if you leave one before the "%d".

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    sorry, forgot to put what my .txt file has:

    3
    1 8888888888 2222222222
    2 9999999999 10000000000
    2 10000000000 9999999999

    I was basically just trying to make sure i remembered correctly how to do fscanf. I have this in other programs and they work, so I'm not seeing why the printf doesn't return 3. Also, my program seems to crash when i close the file as well
    fclose(fin);

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    What if fopen fails? and yes, you should put spaces around %d in fscanf.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    Like tabstop said, check you opened the file successfully.

    For one thing, if you're in the wrong directory, you won't find the file and you'll end up crashing.

    Add some error checks!!!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  2. fscanf causing a crash
    By dougwilliams in forum C Programming
    Replies: 6
    Last Post: 11-18-2007, 04:52 PM
  3. fscanf help
    By Axel in forum C Programming
    Replies: 13
    Last Post: 10-22-2006, 11:44 PM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 02:32 PM
  5. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 07:24 AM

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