0

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?

GManz
  • 1,548
  • 2
  • 21
  • 42

5 Answers5

4

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

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

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.

John Chadwick
  • 3,193
  • 19
  • 19
1

4 * 16 is 64

pmg
  • 106,608
  • 13
  • 126
  • 198
1

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

Mayank
  • 5,454
  • 9
  • 37
  • 60
1

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.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711