ftell returning incorrect value
Asked Answered
R

2

3

I am having a problem where ftell is returning an incorrect value. My code, when run in netbeans on linux reports correctly, but the exact same code, running in netbeans on windows (using mingw) reports incorrectly. the file pointer is to a file opened in BINARY_READ. in my linux netbeans, after running my subroutine, the ftell reports 35. in my windows netbeans, the after calling the same subroutine, the ftell is 3621. I traced through my subroutine and the following statement appears to cause the problem:

if (((header_size = getc (fp)) == EOF) || (header_size == 0))

on my linux netbeans, the ftell(fp) after this statement results in 1. but on my windows netbeans, the ftell(fp) after this statement is 3585.

what could be causing the problem?

Reduction answered 8/10, 2013 at 23:16 Comment(2)
Did you open the file with mode "b"?Symer
Oh Snap.. I had incorrect defines for READ_BINARY and WRITE_BINARY. They were "r" and "w" respectively instead of "rb" and "wb". ThanksReduction
P
9

You need to open the file in binary mode:

fp = fopen(name, "rb");

or similar. You should get in a habit of always doing this, since only binary mode has well-defined behavior in standard C. On POSIX systems, binary and text (default) mode behave the same, but on windows, munging of newlines takes place in a way that messes up file contents and offsets.

Propound answered 8/10, 2013 at 23:31 Comment(0)
C
1

Note that this problem may not have anything to do with binary files, this could be caused by the fact that even 64bit windows machines have a long that's 4 bytes, whereas on linux 64bit systems a long can be either 4bytes or 8bytes.

Since the ftell returns long, under windows the maximum file you can read is 2GB. Whereas under linux, it's higher.

The trick under windows is to use _ftelli64() instead of ftell(), then you will have 64bit access.

Clancy answered 30/11, 2017 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.