PDA

View Full Version : How to find a file pointer position using a file descriptor



mentallysilent
September 9th, 2008, 04:32 PM
Hello,

How do I find out the position of a file pointer using a file descriptor? I'm trying to port some windows code to Linux and the code calls _ftell which comes from io.h
What is the equivalent in Linux? ftell doesn't seem to be it since it doesn't take a file descriptor.

Thanks very much

kjohansen
September 9th, 2008, 04:45 PM
You can do:



#include <fildes.h>
int fd=open(....); //file descriptor
FILE *f = fileptr( fd ); //file pointer

mentallysilent
September 9th, 2008, 04:52 PM
Where does 'fileptr(int)' come from?

kjohansen
September 9th, 2008, 04:55 PM
http://www.thinkage.ca/english/gcos/expl/c/lib/filept.html

mentallysilent
September 9th, 2008, 04:58 PM
Thanks very muuch.

I don't seem to have this header file tho. The include line causes an error:

error: filedes.h: No such file or directory

kjohansen
September 9th, 2008, 05:00 PM
what compiler are you using? And what system are you on?

mentallysilent
September 9th, 2008, 05:04 PM
uname -a: 2.6.22-14-generic #1 SMP i686 GNU/Linux
gcc --version: gcc (GCC) 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)

kjohansen
September 9th, 2008, 05:20 PM
The <fildes.h> solution I grabbed from an old lab from school that ran on the school's unix systems, but I get an error on my ubuntu distro.

My office mate suggested:


#include<stdio.h>
FILE * filePTR=fdopen(int fildes, const char *mode);


mode ='r', 'w'...

Zugzwang
September 9th, 2008, 05:27 PM
You can use the lseek/fseek functions:


int ptr = fseek(file, 0, SEEK_CUR);

mentallysilent
September 9th, 2008, 05:39 PM
fseek takes a FILE* pointer. I only a have an integer file descriptor :(

mentallysilent
September 9th, 2008, 05:44 PM
Thanks. That compiled successfully.