Simple reading

Started by
5 comments, last by Austrian Coder 20 years, 11 months ago
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
Advertisement
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?
#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.
heh. whats the return value of the fread() ?
the return value is 22. (int error = fread (buffer, 1, size, m_pFile)
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 -
-Benny-
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

This topic is closed to new replies.

Advertisement