Determine the current file position
#include <unistd.h> off_t tell( int filedes ); off64_t tell( int filedes );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The tell() function determines the current file position for any subsequent read() or write() operation (that is, any subsequent unbuffered file operation). The filedes value is the file descriptor returned by a successful call to open().
You can use the returned value in conjunction with lseek() to reset the current file position.
The current file position, expressed as the number of bytes from the start of the file, or -1 if an error occurs (errno is set). A value of 0 indicates the start of the file.
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> char buffer[] = { "A text record to be written" }; int main( void ) { int filedes ; int size_written; /* open a file for output */ /* replace existing file if it exists */ filedes = open( "file", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ); if( filedes != -1 ) { /* print file position */ printf( "%ld\n", tell( filedes ) ); /* write the text */ size_written = write( filedes , buffer, sizeof( buffer ) ); /* print file position */ printf( "%ld\n", tell( filedes ) ); /* close the file */ close( filedes ); } return EXIT_SUCCESS; }
produces the output:
0 28
tell() is QNX 4; tell64() is Large-file support
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
chsize(), close(), creat(), dup(), dup2(), eof(), errno, execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), fcntl(), fileno(), fstat(), isatty(), lseek(), open(), read(), sopen(), stat(), umask(), write()