tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

fopen(.., "a") + fseek



Hey,

I wonder if fopen(..., "a") is correct in ignoring any
subsequent seeks 'backwards'. I always thought fopen(..., "a")
would be equivalent of doing fopen(..., "r+"); fseek(.., 0, SEEK_END);
Seems I'm wrong. But is the following intended?

$ cat test.out
1
2
gsuffa
$ cat tst.c
#include <stdio.h>
int main(int argc, char* argv[]) {
        FILE *x; int j=3;
        if (!(x=fopen("test.out", "a"))) return 1;
        if (fseek(x, -7, SEEK_END) < 0) return 2;
        if (fprintf(x, "%d\ngsuffa\n", j) < 0) return 3;
        if (fclose(x) < 0) return 4;
        return 0;
}
$ cc -o tst tst.c
$ ./tst
$ echo $?
0
$ cat test.out
1
2
gsuffa
3
gsuffa
$

Note there were no errors returned from any of the library functions,
but still the fseek obviously failed. If this is correct behaviour,
shouldn't fseek give an error?

Regards,

-Martin

PS: I expected the output to be "1\n2\n3\ngsuffa\n" which it is if
I replace the "a" with "r+" in the above snippet.


Home | Main Index | Thread Index | Old Index