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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File struct /file pointer using fseek problem!

Status
Not open for further replies.

jl3574

Programmer
Jun 5, 2003
76
CA
if i have a file struct HEADER and a file struct BODY which contains the struct HEADER :

typedef struct HEAD
{
int somenumber1;
int somenumber2;
BOOL somebool;
long somenumber3;
} HEADER;

typedef struct BODY
{
HEADER head
int bodynumber1;
int bodynumber2;
}BODYTYPE

say i read a file with this structure like this:

FILE *filename1;
BODYTYPE test;
fread(&test,sizeof(test),1,filename1);

this works fine but what if i want to skip the HEADER and just read the BODY?
iknow i have to find the size of HEAD and then use lseek to move the file pointer then use fread but i'm not quite sure how to code this?
help pls .
 
One thing you should be aware of is that structs often contain "holes" between members to deal with alignment issues. If there are any holes between the header and first body member, you won't be in the correct place in the file.

This should work. Also, I think you want fseek since you're using a file pointer.

fseek(filename1, offsetof(BODYTYPE, bodynumber1), SEEK_CUR);
 
but be cautious using fseek...it doesn't work well with
text mode file operations....
but it works fine with binary mode file operations
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top