Start New Discussion within our Software Development Community

I've seen tons of examples, but I can't seem to find what I need. I'm pretty new to programming, only been doing it for 9 months. Here's what I'm trying to do. I'm loading a file with the below info.

Item1 15 1
Item2 10 1
Item3 1 2
Item4 5 2
Item5 5 2
Item6 1 7
Item7 5 1
Item8 1 2
Item9 1 2
Item10 1 4
Item11 1 4
Item12 10 120

I want to read each line into a array. I definitely want to use a struct, but that is unchartered land that I am trying to get familiar with. Here is my code so far

#include <stdio.h>

struct items
    char itemNumber[10] ;
    int timeToFinish;
    int elapsedMinutes;

int main ()
    char text[100];
    int itemAmount = 0;
    char itemNumber[10];
    int timeToFinish = 0;
    int elapsedMinutes = 0;
    int i = 0;
    int readCounter = 0;
    int finishTime[20];//holds the finish items
    int counter = 0;
    // Prompt the user for a filename.
    printf ("Enter a filename to load: ");
    scanf ("%s" , &text);
    FILE *fn;
    fn = fopen(text, "r");
    // Check to make sure that we opened the file successfully.
    if (fn != NULL) 
        //fscanf (fn, "%d", &itemAmount);//Scan first line into variable
        // Initialize it to NULLs
        for (i=0; i<15; i++) 
            finishTime[i] = 0;
        //Loading each item from the file into our array.
        while (!feof(fn))
                fscanf(fn, "%s %d %d", &itemNumber, &timeToFinish, &elapsedMinutes);
                finishTime[counter] = timeToFinish;
                //finishTime[counter] = elapsedMinutes;
        // Close our file up.
        printf ("\nFile successfully loaded!\n");

        // Print out the array contents.
        for (readCounter = 0; readCounter < 15; readCounter++) 
            if (finishTime[readCounter] != 0) 
                printf("item%d %d %d\n", readCounter, finishTime[readCounter]);

    else {
        // If file failed to open, tell the user.
        printf("Error: The file you specified could not be found!");
    system ("PAUSE");
    return 0;

I have been playing with it for a while, so I figured some outside opinions would be helpful. I have variables itemNumber, timeToFinish, and elapsedMinutes, but I want to connect the struct to the variables and read them in so that I could at any time say I want item4 or item9 and then do with them as I please. Any help for a newb?

This article has been dead for over six months. Start a new discussion instead.