Using structs in fread wont return correct number of bytes read

Started by
2 comments, last by executor_2k2 21 years, 11 months ago
Im using fread with structs to get at some animation data. The following line of code returns the wrong number: fread(pKeyObj->pPosVerts, sizeof(pos_t), pKeyObj->numOfKeysPos, m_FilePointer); This line of code will return the value inside of numOfKeysPos. I would like fread to return sizeof(pos_t) * numOfKeysPos. How can i do this? I wonder if fread is even reading my struct properly becaues it is returning the wrong value. Any ideas? Thanks. [edited by - executor_2k2 on May 3, 2002 9:47:41 PM]
Well, that was a waste of 2 minutes of my life. Now I have to code faster to get 'em back...
Advertisement
FREAD(3)            Linux Programmer's Manual            FREAD(3)NAME       fread, fwrite - binary stream input/outputSYNOPSIS       #include <stdio.h>       size_t  fread(void  *ptr,  size_t size, size_t nmemb, FILE       *stream);       size_t fwrite(const void *ptr, size_t size, size_t  nmemb,       FILE *stream);DESCRIPTION       The function fread reads nmemb elements of data, each size       bytes long, from the stream pointed to by stream,  storing       them at the location given by ptr.       The  function  fwrite  writes nmemb elements of data, each       size bytes long, to  the  stream  pointed  to  by  stream,       obtaining them from the location given by ptr.       For non-locking counterparts, see unlocked_stdio(3).RETURN VALUE       fread  and  fwrite return the number of items successfully       read or written (i.e., not the number of characters).   If       an error occurs, or the end-of-file is reached, the return       value is a short item count (or zero).       fread does not distinguish between end-of-file and  error,       and  callers  must  use feof(3) and ferror(3) to determine       which occurred. 


[edited by - Null and Void on May 3, 2002 9:56:52 PM]
Don''t read structs from files you haven''t created with the same program!

Do not make any assumptions about how a struct is laid out in memory vs. how it is in a file unless your very same program created the file.

You must pay attention to the exact file specification and read that specification independent of a struct''s layout in memory.
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
Since fread return numbers of item read, how about u change a little in your function call:
byteread = fread(pKeyObj->pPosVerts, 1, sizeof(pos_t) * pKeyObj->numOfKeysPos, m_FilePointer); 
"after many years of singularity, i'm still searching on the event horizon"

This topic is closed to new replies.

Advertisement