Mail Archives: djgpp-workers/2001/04/04/10:58:23
Eli said:
> differently. Can someone please check what happens on Unix and
> GNU/Linux systems? A simple test program which opens an existing file
> with "a" and then calls ftell and prints the result is all you need.
----- c.c starts. -----
#include <errno.h>
#include <stdio.h>
int main(void)
{
FILE *f;
f = fopen(__FILE__, "a");
if( f == NULL )
{
fprintf(stderr, "fopen failed, errno = %d.\n", errno);
exit( 1 );
}
printf("1, ftell(f) = %ld.\n", ftell(f));
fclose(f);
f = fopen(__FILE__, "a+");
if( f == NULL )
{
fprintf(stderr, "fopen failed, errno = %d.\n", errno);
exit( 2 );
}
printf("2, ftell(f) = %ld.\n", ftell(f));
fclose(f);
return( 0 );
}
----- c.c ends. -----
; uname -a
SunOS lws256 5.6 Generic_105181-23 sun4u sparc
; l c.c
-rw-r----- 1 eplmst erisoft 466 Apr 4 16:44 c.c
; ./c
1, ftell(f) = 466.
2, ftell(f) = 0.
> The Posix draft seem to say the same. It would be interesting to know
> what happens with open and fdopen on Unix systems, btw.
----- d.c starts. -----
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(void)
{
FILE *f;
int fd;
fd = open(__FILE__, O_RDONLY|O_APPEND);
if( f < 0 )
{
fprintf(stderr, "open failed, errno = %d.\n", errno);
exit( 1 );
}
printf("1, lseek(fd, 0, SEEK_CUR) = %ld.\n", lseek(fd, 0, SEEK_CUR));
f = fdopen(fd, "a");
if( f == NULL )
{
fprintf(stderr, "fdopen failed, errno = %d.\n", errno);
exit( 3 );
}
printf("1, ftell(f) = %ld.\n", ftell(f));
fclose(f);
close(fd);
fd = open(__FILE__, O_RDWR|O_APPEND);
if( f == NULL )
{
fprintf(stderr, "open failed, errno = %d.\n", errno);
exit( 2 );
}
printf("2, lseek(fd, 0, SEEK_CUR) = %ld.\n", lseek(fd, 0, SEEK_CUR));
f = fdopen(fd, "a+");
if( f == NULL )
{
fprintf(stderr, "fdopen failed, errno = %d.\n", errno);
exit( 4 );
}
printf("2, ftell(f) = %ld.\n", ftell(f));
fclose(f);
close(fd);
return( 0 );
}
----- d.c ends. -----
; ./d
1, lseek(fd, 0, SEEK_CUR) = 0.
1, ftell(f) = 1004.
2, lseek(fd, 0, SEEK_CUR) = 0.
2, ftell(f) = 1004.
Right,
MartinS
- Raw text -