Archived

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

File handling procedures

This topic is 5751 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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