943,928 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1524
  • C RSS
Jan 7th, 2009
0

structure and reading a text file

Expand Post »
hi,
how can i read a text file "textfile.txt" with a structure? (in C)
thanks



the structure is in this form.
  1. typedef struct list
  2. {
  3. char ID[9];
  4. char YR[10];
  5. char lname[14];
  6. char fname[24];
  7. }LIST[256];
Last edited by Ancient Dragon; Jan 7th, 2009 at 10:34 am. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
benchCUB is offline Offline
3 posts
since Jan 2009
Jan 7th, 2009
0

Re: structure and reading a text file

Don't know the answer to your question until I know the contents of the text file. Without that knowledge, generally I would use fgets() to read a line, then parse it into its individual parts into the structure.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 7th, 2009
0

Re: structure and reading a text file

the text file " textfile.txt" is in this form:

076 yr1 Santos Jose
054 yr1 Alonzo Pedro
087 yr4 Vasquez Carlos Israel
076 yr2 Marquez Juan Miguel
Reputation Points: 10
Solved Threads: 0
Newbie Poster
benchCUB is offline Offline
3 posts
since Jan 2009
Jan 7th, 2009
0

Re: structure and reading a text file

you could do something like this:
  1. fscanf(fp,"%s%s%s", List[i].ID, List[i].YR, List[i].lname);
  2. // get the first name separately because it may be composed of
  3. // two or more names with spaces, so fscanf() will not work with this.
  4. fgets(List[i].fname, sizeof(List[i].fname, fp);

Of course you will have to put the above in a loop after opening the file for reading.
Last edited by Ancient Dragon; Jan 7th, 2009 at 12:04 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 7th, 2009
0

Re: structure and reading a text file

i have to read two text file
"textfile.txt" and "textfile2.txt"

it only reads "textfile.txt"

how to join the two text and read it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
benchCUB is offline Offline
3 posts
since Jan 2009
Jan 8th, 2009
0

Re: structure and reading a text file

Click to Expand / Collapse  Quote originally posted by benchCUB ...
i have to read two text file
"textfile.txt" and "textfile2.txt"

it only reads "textfile.txt"

how to join the two text and read it.
Open both text files and just read each of them. Only you can answer your question because we have no idea what the hell you are talking about.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

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: Problem with array of pointers
Next Thread in C Forum Timeline: Wireless networks





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


Follow us on Twitter


© 2011 DaniWeb® LLC