Archived

This topic is now archived and is closed to further replies.

MindCode

File handling procedures

Recommended Posts

MindCode    122
I know this is going to sound like a newbie question but it''s been bugging my brain for a while now. When I parse a file using stdio.h like this: #define BUFFERSIZE 100 struct astr { char bdata; short wdata; }; int main() { FILE *f = fopen("file.dat","rb"); astr buf[BUFFERSIZE]; fread(buf,sizeof(astr),BUFFERSIZE,f); fclose(f); // and do whatever... } Since Dev-C++/MinGW packs bytes between elements in it''s data structures, does "fread" then account for this by skipping bytes in the buffer based on the fact that I wrote it like: fread(buf,sizeof(astr),BUFFERSIZE,f); Because I''ve also seen many times: fread(buf,sizeof(astr)*BUFFERSIZE,1,f); What''s the difference here? I know that I can use: struct astr { char bdata __attribute__((packed)); short wdata __attribute__((packed)); }; //... fread(buf,sizeof(astr)*BUFFERSIZE,1,f); To have it read the file correctly, but this really slows down the member accesses. I would really like to know of a way to skip the bytes at read time so that I don''t have to use "attribute".

Share this post


Link to post
Share on other sites
LessBread    1415
quote:
Original post by MindCode

fread(buf,sizeof(astr),BUFFERSIZE,f);

fread(buf,sizeof(astr)*BUFFERSIZE,1,f);

What''s the difference here?



In the first case you''re reading 100 elements each the size of astr bytes. In the second, you''re reading 1 element the size of astr*100.

I can''t speak to the particulars of the compiler, but I can tell you that "__attribute__((packed));" isn''t portable.

Share this post


Link to post
Share on other sites