Thread: File Input

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    5

    File Input

    I have a question regarding reading input from a file.

    The input file (input.txt):
    Code:
    8 9 2.5
    10 5 2.6
    7 15 9.5
    0 0 0
    1
    1 8
    My program:
    Code:
    #include <stdio.h>
    
    #define MAX_TBL_SIZE 30
    
    int main()
    {
        FILE *ifp;
        
        char name[10];
        int index = 0;
        int ind2;
        int id[MAX_TBL_SIZE], stock[MAX_TBL_SIZE];
        float price[MAX_TBL_SIZE];
        
        // Reads user input for filename. 10 character long string. Guaranteed to be correct filename.
        printf("Enter the name of the input file:\n");
        scanf("&#37;s", name);
        printf("\n");
    
        // Opens the filename entered
        ifp = fopen(name, "r");
        
        // Adds variables from file into specified arrays
        while(id[index] != 0 && stock[index] !=0 && price[index] != 0.0)
        {
                        fscanf(ifp, "%d", &id[index]);
                        fscanf(ifp, "%d", &stock[index]);
                        fscanf(ifp, "%f", &price[index]);
                        
                        index++;
        }
        
         
        // Prints current index value
        printf("DEBUGGER: INDEX = %d\n", index);
        
        // Prints each value of array id untill it reaches the index vaule
        for (ind2 = 0; ind2 < index; ind2++)
        {
            printf("DEBUGGER: id[%d] = %d", ind2, id[ind2]);
            printf("\n");
        }
        
        // Same as above, except for stock array
        for (ind2 = 0; ind2 < index; ind2++)
        {
            printf("DEBUGGER: stock[%d] = %d", ind2, stock[ind2]);
            printf("\n");
        }
        
        // Repeated for price array
        for (ind2 = 0; ind2 < index; ind2++)
        {
            printf("DEBUGGER: price[%d] = %.2f", ind2, price[ind2]);
            printf("\n");
        }
        
    
         // Shows current location in file
         fscanf(ifp, "%d", &num);
         printf("CURRENT LOCATION fscanf: %d\n", num);
    
        // Closes file
        fclose(ifp);
    
        system("PAUSE");
        return 0;
    }
    What I want my program to do is to add these variables to 3 different arrays, but stop once it reaches the three zero string (0 0 0).

    When it compiles, it seems my index value is stopping too soon. Each time it only seems to accumulate to 2 before stopping, rather than stopping once it reaches the marker.

    Any ideas on what is going on here?
    Last edited by Mcwaffle; 11-10-2007 at 01:24 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your index++ means you're always comparing the junk in your arrays (which are uninitialised, so it's just random what happens), and not comparing the last 3 values read from the file.

    I would suggest reading the FAQ on how to use fgets() and sscanf()
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM