Sign in to follow this  

Convert fstream file handle to FILE *fp??

This topic is 3577 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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>"

Share this post


Link to post
Share on other sites
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*.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3577 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this