954,153 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

structure and reading a text file

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

the structure is in this form.

typedef struct list
{
           char ID[9];
           char YR[10];
           char lname[14];
           char fname[24];
}LIST[256];
benchCUB
Newbie Poster
3 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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

benchCUB
Newbie Poster
3 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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.

benchCUB
Newbie Poster
3 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You