1,105,427 Community Members

read file into struct array

Member Avatar
ITapprentice
Newbie Poster
4 posts since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

12
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;
                counter++;
        }
        
        // Close our file up.
        printf ("\nFile successfully loaded!\n");
        fclose(fn);

        // 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?

Member Avatar
iamthwee
Posting Sage
7,081 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 596 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

I would begin by reading the following tutorial:
http://www.daniweb.com/tutorials/tutorial45806.html

Don't use !feof to read files.
read_me

Then consult a struct tutorial with google.

You might also want to find out how to convert a char array[] to an int.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article