944,043 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1076
  • C RSS
Apr 7th, 2007
0

Reading data from a textfiletype

Expand Post »
Im trying to read some integers values from a text file.
The problem is that first lines of the text files contains some text and only then theres the values that i want to read into variables , and i dont know how to get down three lines and then read using fscanf .

TEXT FILE(storeinfo.txt):
Stores Information:
Store Code Store Size Store Workers Store Income
951 1200 7 67000
952 1452 12 92833
953 800 5 41000
954 1000 6 61000
955 1500 12 100321

Code:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct
  4. {
  5. int code;
  6. int size;
  7. int workers;
  8. int total;
  9. }store;
  10. int main()
  11. {
  12. int i;
  13. FILE *filein;
  14. char source[40];
  15. store storearr[5]={0};
  16. printf ("Enter a source filename: ");
  17. gets(source);
  18. filein = fopen (source,"rt");
  19. if (filein == NULL)
  20. {
  21. printf ("Unable to open sourcefile...\n");
  22. exit(1);
  23. }
  24. for (i=0;i<5;i++)
  25. fscanf(filein,"%d %d %d %d\n",&storearr[i].code,&storearr[i].size,&storearr[i].workers,&storearr[i].total);
  26. for (i=0;i<5;i++)
  27. printf ("%-15d %-15d %-15d %-15d\n",storearr[i].code,storearr[i].size,storearr[i].workers,storearr[i].total);
  28. fclose (filein);
  29. return 0;
  30. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
RisTar is offline Offline
51 posts
since Feb 2007
Apr 7th, 2007
0

Re: Reading data from a textfiletype

Keep a counter which will be initialized to 1, fgets the entire line of text in a character array, skip the first two lines using the counter previously mentioned and start reading from the third line.

Read this for using fgets.
Last edited by ~s.o.s~; Apr 7th, 2007 at 6:52 am.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 7th, 2007
0

Re: Reading data from a textfiletype

can you please post a code example ?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
RisTar is offline Offline
51 posts
since Feb 2007
Apr 7th, 2007
1

Re: Reading data from a textfiletype

#include <stdio.h> 
#include <stdlib.h> 

#define MYFILE "c:\\example.txt" 

int main(void)
{
  FILE *fp;
  char buf[BUFSIZ] = "blah";
  int i;
  
  if ((fp = fopen(MYFILE, "r")) == NULL)
  {
    perror (MYFILE);
    return (EXIT_FAILURE);
  }
  
  i = 0;

  while (fgets(buf, sizeof(buf), fp) != NULL)
  {
    if (i==0 || i == 1)      
      printf("Skip these\n");
    else
      printf ("Line %2d: %s", i, buf);
      i++;
  }
  
  fclose(fp);
    getchar();
  return(0);
}
Last edited by iamthwee; Apr 7th, 2007 at 8:33 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 7th, 2007
0

Re: Reading data from a textfiletype

Thanks for the help guys !
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
RisTar is offline Offline
51 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: type conversion
Next Thread in C Forum Timeline: moving folder





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC