Home > Net >  Get FILE stream from File Handle
Get FILE stream from File Handle

Time:12-27

I would like to use file stream functions (e.g., "ftell") on an existing file I opened using CreateFile (which returns a HANDLE). I searched SO and elsewhere with no joy. I want to do something like this:

HANDLE h;
FILE *f;
int pos;
h = CreateFile( "MYFILE.TXT", ... )
f = *convert*( h, &f );    // the function I'm looking for  
pos = ftell( f );

Is there a way to get a FILE stream from a file handle?

CodePudding user response:

Use _open_osfhandle() to create a C-style file descriptor from a Win32 HANDLE, and then use _fdopen() to create a FILE* from the file descriptor.

CodePudding user response:

Converting a windows HANDLE won't work because it's specific for windows only, whereas FILE is cross-platform.

Try instead to get an alternative function to ftell in winapi like here.

  • Related