Archived

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

Simple reading

This topic is 5314 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

Hi! I have here a stupit and easy(?) mistake in my source, but where? FILE* pf; pf = fopen("a file", "rb"); Now i want to jump at position 20 and read out a 22 big buffer. char* buffer = 0; unsigned int DataOffset = 20; unsigned int Size = 22; buffer = (char*) malloc (Size); rewind (pf); fseek (pf, DataOffset, SEEK_SET); // read buffer fread (buffer, 1, Size, pf); And close it finally flcose(pf); The result is a 3 element big buffer, it should be 22 elements big, with the wrong stuff in it. Where could be the mistake? Thanks, Christian

Share this post


Link to post
Share on other sites
the rewind and fseek are redundant.

other than that,

#1, how do you figure its only reading 3 bytes?
#2, is the files bigger than 42 bytes?
#3, any cosmic ray activity in your area?

Share this post


Link to post
Share on other sites
#1, how do you figure its only reading 3 bytes?

strlen(buffer) -> 3

#2, is the files bigger than 42 bytes?

yes. it is 58 bytes big.

#3, any cosmic ray activity in your area?

I dont know realy, but i think there is something with the mon.

Share this post


Link to post
Share on other sites
You are reading a binary file (at least that''s how you''re opening the file). You can''t count bytes read with strlen because at the first ''\0'' it will stop counting and return the result. In this case the 4th byte probably is ''\0'' and strlen returns STRING length 3.

So the moon is probably ok even there.

- Benny -

Share this post


Link to post
Share on other sites
Hmmm... so what shall i do now? If i look at the buffer, which should contains this:
Hello - this is a test.

If I open the binary file with the editor i get this:
¤@C D@C ·Ñ8 @C  ÌÌÌÌHello - this is a test

Share this post


Link to post
Share on other sites