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];

Recommended Answers

All 5 Replies

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.

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

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.