RSS Forums RSS

structure and reading a text file

Please support our C advertiser: Programming Forums
Thread Solved
Reply
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

  #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 9:34 am. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Posts: 13,883
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: 1231
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: structure and reading a text file

  #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.
Reply With Quote  
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

  #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  
Posts: 13,883
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: 1231
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: structure and reading a text file

  #4  
Jan 7th, 2009
you could do something like this:
fscanf(fp,"%s%s%s", List[i].ID, List[i].YR, List[i].lname);
  // get the first name separately because it may be composed of 
  // two or more names with spaces, so fscanf() will not work with this.
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 11:04 am.
Reply With Quote  
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

  #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  
Posts: 13,883
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: 1231
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: structure and reading a text file

  #6  
Jan 7th, 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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 682 | Replies: 5 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:54 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC