fscanf issue!

This is a discussion on fscanf issue! within the C Programming forums, part of the General Programming Boards category; Hi all! I'm learning how to separate a program into functions and have come accross a strange problem which works ...

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Angry fscanf issue!

    Hi all!

    I'm learning how to separate a program into functions and have come accross a strange problem which works before function breakdown, but not after!

    Please find my code below, the problem is that my file header (10) is read as 1 - missing the 0!

    Therefore, the array is created with storage for a single member! Any help will be much appreciated! :^)

    Code:
    //////////////////////////////////////////////////////////////////////////
    // Program Title	    : Tutorial 8, Exercise 3
    // Author Name		    : Joe
    // Date of Creation	    : 17/05/2011
    // Date of Last Edit 	    : 17/05/2011
    //////////////////////////////////////////////////////////////////////////
    
    //////////////////////////////////////////////////////////////////////////
    // Pre-processor Directives
    //////////////////////////////////////////////////////////////////////////
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define FILE_NAME "G:\\numbers.txt"
    
    //////////////////////////////////////////////////////////////////////////
    // Type Definitions
    //////////////////////////////////////////////////////////////////////////
    
    //////////////////////////////////////////////////////////////////////////
    // Function Prototypes
    //////////////////////////////////////////////////////////////////////////
    
    void display(int [], int);
    int load_head();
    void load_array(int *, int);
    
    //////////////////////////////////////////////////////////////////////////
    // Main Function Implementation
    //////////////////////////////////////////////////////////////////////////
    
    int main(void)
    {
        int header = load_head();
    
        printf("\nHeader = %d", header);
    
        int array[header], i;
    
        load_array(array, header);
    
        display(array, header);
    
        int target;
    
        printf("\nEnter target: ");
        scanf("%d", &target);
    
        for(i = 0; i < header; i++) {
            if(target < header) {
                printf("\n%d is not present!", target);
                break;
            } else if(target == array[i]) {
                printf("\n%d is at position %d", target, i + 1);
            }
        }
    
        return 0;
    }
    
    //////////////////////////////////////////////////////////////////////////
    // Sub-function Implementations
    //////////////////////////////////////////////////////////////////////////
    
    void display(int a[], int n)
    {
        int i;
    
        for(i = 0; i < n; i++) {
            printf("\n%d", a[i]);
        }
    }
    
    //////////////////////////////////////////////////////////////////////////
    
    int load_head()
    {
        int a;
        FILE *fp;
    
        fp = fopen(FILE_NAME, "r");
        if(fp == NULL) {
            printf("\nCannot open %s", FILE_NAME);
            exit(EXIT_FAILURE);
        }
    
        return fscanf(fp, "%d", &a);
    
        fclose(fp);
    }
    
    //////////////////////////////////////////////////////////////////////////
    
    void load_array(int *m, int a)
    {
        int i;
        FILE *fp;
    
        fp = fopen(FILE_NAME, "r");
        if(fp == NULL) {
            printf("\nCannot open %s", FILE_NAME);
            exit(EXIT_FAILURE);
        }
    
        fseek(fp, sizeof(i), SEEK_SET);
    
        for(i = 0; i < a; i++) {
            fscanf(fp, "%d", &m[i]);
        }
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Read what fscanf() does before using it. OK?
    http://www.manpagez.com/man/3/scanf/
    After your function returns fclose() is never called. It's dead code.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... you're breaking things down too far... load_head and load_array should probably be one LoadFile function... because they both contribute to the same goal... loading the array from the file.

    You actual problem is that you are scanning for a in load_head but returning the result from fscanf()... which will be 1 because it converted one value.

    Also since your return statement is before fclose() the file never gets closed.

    Try this...
    Code:
    int load_head(char* FileName)
    {
        int a;
        FILE *fp;
    
        fp = fopen(FileName, "r");
        if(fp == NULL) {
            printf("\nCannot open %s", FileName);
            exit(EXIT_FAILURE);
        }
    
        fscanf(fp, "%d", &a);
    
        fclose(fp);
    
        return a; 
    }
    Now when you call load_array the fopen() could fail and you have the wrong size.

    By combining the two functions you A) open the file once B) read it sequentially C) close it once ... I'm thinking that's a much better approach.


    Also in main() you are doing this...
    Code:
        int header = load_head();
    
        int array[header], i;
    You should know this is compiler specific behavior. Only C-99 compliant compilers will support it.
    This will produce errors on any prior version.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fscanf segmentation fault issue (I think)
    By zouru in forum C Programming
    Replies: 5
    Last Post: 04-03-2011, 03:00 PM
  2. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  3. fscanf() issue...
    By smoking81 in forum C Programming
    Replies: 12
    Last Post: 09-08-2008, 06:56 AM
  4. fscanf Issue
    By Maser in forum C Programming
    Replies: 3
    Last Post: 04-23-2007, 07:42 PM
  5. FSCANF format string issue
    By INFERNO2K in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2005, 05:52 PM

Tags for this Thread


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