Question

I am new to C and I have this code:

f = fopen( argv[1], "rb" );
fseek( f, 64, SEEK_SET );
fpos_t pos;
fgetpos (f, &pos);
printf("%x", pos);

However, this returns 40, even though it's supposed to be returning 64. What am i doing wrong?

Was it helpful?

Solution

Because you're using %x. It's saying 40 as in 0x40, the hexadecimal number. You need %i or %d to get a decimal number.

OTHER TIPS

You are outputting 64 in hex format, "%x". Since 64=0x40, the mystery is solved!

4 * 16 is 64

whatever you are printing is in hex format. 40 in decimal is 64. Do you mean the file size is 0x64 or 0x40

fpos_t is not (necessarily) an arithmetic type and cannot be used with printf. An implementation could even store it as a structure containing an encrypted position if it liked. Use ftell (or ftello if available) to get the file offset in a meaningful numeric form. fgetpos is largely useless.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top