Sign in to follow this  
faculaganymede

Convert fstream file handle to FILE *fp??

Recommended Posts

SiCrane    11839
Not in standard C++. Your compiler/standard library implementation may offer ways to get a FILE * from an fstream, but it's not common.

Share this post


Link to post
Share on other sites
faculaganymede    145
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
jyk    2094
Quote:
...but I need to use a function that requires FILE *fp as input.
Just out of curiosity, what is the function?

Share this post


Link to post
Share on other sites
SiCrane    11839
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?

Share this post


Link to post
Share on other sites
Sneftel    1788
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
faculaganymede    145
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
Antheus    2409
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
the_edd    2109
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?

Share this post


Link to post
Share on other sites
faculaganymede    145
Quote:
Original post by the_edd
Do multiple smaller seeks work?

Thanks. I was thinking about that. I am going to try keeping track of and seek from current position rather than from beginning of the file.

Share this post


Link to post
Share on other sites

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