structure and reading a text file

Thread Solved

Join Date: Jan 2009
Posts: 3
Reputation: benchCUB is an unknown quantity at this point 
Solved Threads: 0
benchCUB benchCUB is offline Offline
Newbie Poster

structure and reading a text file

 
0
  #1
Jan 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: structure and reading a text file

 
0
  #2
Jan 7th, 2009
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: benchCUB is an unknown quantity at this point 
Solved Threads: 0
benchCUB benchCUB is offline Offline
Newbie Poster

Re: structure and reading a text file

 
0
  #3
Jan 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: structure and reading a text file

 
0
  #4
Jan 7th, 2009
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: benchCUB is an unknown quantity at this point 
Solved Threads: 0
benchCUB benchCUB is offline Offline
Newbie Poster

Re: structure and reading a text file

 
0
  #5
Jan 7th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: structure and reading a text file

 
0
  #6
Jan 8th, 2009
Originally Posted by benchCUB View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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