Reading data from a textfiletype

Thread Solved
Reply

Join Date: Feb 2007
Posts: 30
Reputation: RisTar is an unknown quantity at this point 
Solved Threads: 0
RisTar RisTar is offline Offline
Light Poster

Reading data from a textfiletype

 
0
  #1
Apr 7th, 2007
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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Reading data from a textfiletype

 
0
  #2
Apr 7th, 2007
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 30
Reputation: RisTar is an unknown quantity at this point 
Solved Threads: 0
RisTar RisTar is offline Offline
Light Poster

Re: Reading data from a textfiletype

 
0
  #3
Apr 7th, 2007
can you please post a code example ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
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: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Reading data from a textfiletype

 
1
  #4
Apr 7th, 2007
#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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 30
Reputation: RisTar is an unknown quantity at this point 
Solved Threads: 0
RisTar RisTar is offline Offline
Light Poster

Re: Reading data from a textfiletype

 
0
  #5
Apr 7th, 2007
Thanks for the help guys !
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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