GManz
GManz

Reputation: 1669

c fgetpos giving wrong position

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?

Upvotes: 0

Views: 323

Answers (5)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215547

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.

Upvotes: 1

Mayank
Mayank

Reputation: 5738

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

Upvotes: 1

John Chadwick
John Chadwick

Reputation: 3213

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.

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 613461

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

Upvotes: 4

pmg
pmg

Reputation: 108986

4 * 16 is 64

Upvotes: 1

Related Questions