Convert fstream file handle to FILE *fp??

Started by
10 comments, last by faculaganymede 16 years, 1 month ago
Does anyone know if there's a way to get a FILE *fp file pointer from a fstream file handle? Thanks.
Advertisement
Not in standard C++. Your compiler/standard library implementation may offer ways to get a FILE * from an fstream, but it's not common.
No. The stream isn't necessarily implemented in terms of a FILE*.

Why do you think you want to do this?
Quote:Original post by Zahlman
Why do you think you want to do this?


The reason is because my application currently does all the file I/Os using the fstream library, but I need to use a function that requires FILE *fp as input.

What about getting the file descriptor from fstream?

According to the documentation, I should be able to do fstream.fd, but the compiler complains:
"error C2039: 'fd' : is not a member of 'std::basic_fstream<_Elem,_Traits>"
Quote:...but I need to use a function that requires FILE *fp as input.
Just out of curiosity, what is the function?
You realize that you're looking at the MSVC 6 documentation for the fstream class in the fstream.h header, not the standard library version in the fstream (no .h) header?
FILE* is NOT the low-level file handle interface to the operating system. It's a type which was used in the C standard libary for the C standard library IO functions. Under UNIX, both fstream and FILE* probably use file descriptors internally. Under Windows, both fstream and FILE* probably use an HFILE internally. In both cases, if you're using fstream, there is no FILE* created behind the scenes, just as there is no fstream created behind the scenes when you have a FILE*.
Thanks for all your feedbacks.

I am still using VS 6.0 on Windows due to having to work with legacy code :(

All my file I/Os are currently done using the fstream library.

I need to use the following function to access a very large image file (>2GB, fstream.seekg doesn't work):
__int64 _lseeki64(
int fd,
__int64 offset,
int origin
);

I couldn't get fd from fstream, so I thought may be there's a way to convert fstream to FILE*fp and use the following function to get fd:
int _fileno(
FILE *stream
);

(Please don't tell me I have to rewrite all the file I/O functions in order to use _lseeki64.)
Quote:Original post by faculaganymede

I couldn't get fd from fstream, so I thought may be there's a way to convert


That function takes HFILE, which is Windows handle. It's completely unrelated to FILE, which is a C struct.

Quote:(Please don't tell me I have to rewrite all the file I/O functions in order to use _lseeki64.)


Nope. You'll need to rewrite it to use the *Ex functions from Windows API. That, or compile your application as 64-bit.

If you did the proper abstraction, and based your classes on std::istream or even std::stream, you might get away with implementing that interface using the *Ex functions.
Quote:Original post by faculaganymede
I need to use the following function to access a very large image file (>2GB, fstream.seekg doesn't work)


Do multiple smaller seeks work?

This topic is closed to new replies.

Advertisement