| | |
read file into struct array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2007
Posts: 4
Reputation:
Solved Threads: 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
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?
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
C Syntax (Toggle Plain Text)
#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?
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
You might also want to find out how to convert a char array[] to an int.
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.
Last edited by iamthwee; Oct 7th, 2007 at 3:16 pm. Reason: editing daft suggestion
*Voted best profile in the world*
![]() |
Similar Threads
- Setting an array and reading in a txt file backwards (C++)
- Reading file input into an array (C++)
- C++ How can read from file.txt & where can save this file(file.txt) to start reading (C++)
- Storing file data in an array? (C++)
- Read in a file and store in char array (C)
- reading txt file into array (C++)
- Help Reading Info in Text File Into an Array (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
Other Threads in the C Forum
- Previous Thread: personal expense book keeping software
- Next Thread: Compiling Multiple files in a single Project
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h






