#1558 ftell() returns 0 file pointer after fopen() for append

WSL
closed
nobody
None
wont-fix
Behaves_as_Documented
2013-01-21
2011-06-22
Veiokej
No

If you open an existing nonnull file for append, ftell() returns 0. However, writes to the file are appended to the end, so it's clear that 0 is the wrong value. Only by doing an explicit fseek() to the end of the file, does ftell() then return the truth. Here's a quick demo C program to show the problem. Compile it and run like "test my_test_file". Obviously there are lots of error cases that it doesn't handle, so be careful.

----------------------------------------------------------------------------------------------------
// Compile with: gcc -o test.exe test.c

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[]){
char *filename;
FILE *handle;
int ftell_position;

filename=argv[1];
handle=fopen(filename,"ab");
ftell_position=ftell(handle);
printf("ftell() says that the file pointer is %d after open for append.\n",ftell_position);
fseek(handle,0L,SEEK_END);
ftell_position=ftell(handle);
printf("ftell() says that the file pointer is %d after seek to end.\n",ftell_position);
return 0;
}
----------------------------------------------------------------------------------------------------

The output looks something like this:

----------------------------------------------------------------------------------------------------
ftell() says that the file pointer is 0 after open for append.
ftell() says that the file pointer is 54410 after seek to end.
----------------------------------------------------------------------------------------------------

gcc -v returns:

----------------------------------------------------------------------------------------------------
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/progra~1/mingw/bin/../libexec/gcc/mingw32/4.5.2/lto-wrapp
er.exe
Target: mingw32
Configured with: ../gcc-4.5.2/configure --enable-languages=c,c++,ada,fortran,obj
c,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgo
mp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-r
untime-libs --disable-werror --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.5.2 (GCC)
----------------------------------------------------------------------------------------------------

ld -v returns:

----------------------------------------------------------------------------------------------------
GNU ld (GNU Binutils) 2.21
----------------------------------------------------------------------------------------------------

I'm running on 32-bit WinXP.

Discussion

  • Earnie Boyd

    Earnie Boyd - 2011-06-22
    • milestone: --> Behaves_as_Documented
    • status: open --> closed-wont-fix
     
  • Earnie Boyd

    Earnie Boyd - 2013-01-21
    • status: closed-wont-fix --> closed
    • resolution: --> wont-fix
    • category: --> Behaves_as_Documented
    • milestone: Behaves_as_Documented --> WSL
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks