Archived

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

executor_2k2

Using structs in fread wont return correct number of bytes read

Recommended Posts

executor_2k2    122
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]

Share this post


Link to post
Share on other sites
Null and Void    1088

FREAD(3) Linux Programmer's Manual FREAD(3)

NAME
fread, fwrite - binary stream input/output
SYNOPSIS
#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]

Share this post


Link to post
Share on other sites
bishop_pass    109
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.

Share this post


Link to post
Share on other sites
DerekSaw    243
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);

Share this post


Link to post
Share on other sites