DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   structure and reading a text file (http://www.daniweb.com/forums/thread166725.html)

benchCUB Jan 7th, 2009 5:19 am
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];

Ancient Dragon Jan 7th, 2009 10:37 am
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.

benchCUB Jan 7th, 2009 11:52 am
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

Ancient Dragon Jan 7th, 2009 12:04 pm
Re: structure and reading a text file
 
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.

benchCUB Jan 7th, 2009 4:59 pm
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.

Ancient Dragon Jan 8th, 2009 12:29 am
Re: structure and reading a text file
 
Quote:

Originally Posted by benchCUB (Post 773144)
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.


All times are GMT -4. The time now is 6:50 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC