read file into struct array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 4
Reputation: ITapprentice is an unknown quantity at this point 
Solved Threads: 0
ITapprentice ITapprentice is offline Offline
Newbie Poster

read file into struct array

 
0
  #1
Oct 7th, 2007
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

  1. #include <stdio.h>
  2.  
  3. struct items
  4. {
  5. char itemNumber[10] ;
  6. int timeToFinish;
  7. int elapsedMinutes;
  8. };
  9.  
  10. int main ()
  11. {
  12. char text[100];
  13. int itemAmount = 0;
  14. char itemNumber[10];
  15. int timeToFinish = 0;
  16. int elapsedMinutes = 0;
  17. int i = 0;
  18. int readCounter = 0;
  19. int finishTime[20];//holds the finish items
  20. int counter = 0;
  21.  
  22. // Prompt the user for a filename.
  23. printf ("Enter a filename to load: ");
  24. scanf ("%s" , &text);
  25.  
  26. FILE *fn;
  27. fn = fopen(text, "r");
  28.  
  29. // Check to make sure that we opened the file successfully.
  30. if (fn != NULL)
  31. {
  32. //fscanf (fn, "%d", &itemAmount);//Scan first line into variable
  33.  
  34. // Initialize it to NULLs
  35. for (i=0; i<15; i++)
  36. {
  37. finishTime[i] = 0;
  38. }
  39.  
  40. //Loading each item from the file into our array.
  41. while (!feof(fn))
  42. {
  43. fscanf(fn, "%s %d %d", &itemNumber, &timeToFinish, &elapsedMinutes);
  44. finishTime[counter] = timeToFinish;
  45. //finishTime[counter] = elapsedMinutes;
  46. counter++;
  47. }
  48.  
  49. // Close our file up.
  50. printf ("\nFile successfully loaded!\n");
  51. fclose(fn);
  52.  
  53. // Print out the array contents.
  54. for (readCounter = 0; readCounter < 15; readCounter++)
  55. {
  56. if (finishTime[readCounter] != 0)
  57. {
  58. printf("item%d %d %d\n", readCounter, finishTime[readCounter]);
  59. }
  60. }
  61. }
  62.  
  63. else {
  64. // If file failed to open, tell the user.
  65. printf("Error: The file you specified could not be found!");
  66. }
  67.  
  68. system ("PAUSE");
  69. return 0;
  70. }

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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: read file into struct array

 
0
  #2
Oct 7th, 2007
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.
Last edited by iamthwee; Oct 7th, 2007 at 3:16 pm. Reason: editing daft suggestion
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC