问题
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/19260209/ftell-returning-incorrect-value