Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to put info from a file into a C struct?

Status
Not open for further replies.

jl3574

Programmer
Jun 5, 2003
76
CA
i have a simple C question with file struct
if i create a simple struct

struct person_info
{
char firstname[10];
char lastname[10];
int age;
}

if i have a binary file with this exact structure and i want to put the info onthe file into this struct how would i do so?
i know i have to create a file pointer
FILE *filname
filename=fopen("personInfoFile","r");
i would open the file for reading and then what? help...
thx
 
Code:
struct person_info pi;
FILE* pfile = fopen("personInfoFile", "r");
// read one structure from the file
fread( &pi, sizeof(pi),1, pfile);

-pete
 
thx pete
one more question
what if i just want to reference to just one of the feilds in the struct from the file?
- john
 
> what if i just want to reference to just one of the feilds in the struct from the file?
Normally, you would just fread()/fwrite() the complete record as a single entity, then access individual fields within memory.

 
What Salem said, otherwise you have to manage the file pointer postion (fseek(), fsetpos())and read the correct number of bytes from the file.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top