I'm fairly new to C++ programming and facing an issue. I have an istream object and i need to link it to a C library which requires FILE* pointer. Is there a way to extract a FILE* from istream object, if not what is the best way to solve this problem?
Asked
Active
Viewed 61 times
1
-
Check out cppreference.com. AFAIK, there is no guaranteed way to achieve that, in particular not from an `istream` but also not from and `ifstream`. – Ulrich Eckhardt Mar 17 '22 at 09:15
-
1Streams are a completely different way to access files. If you want to load a file for C interop, they're not suited. There are alternatives (like the original C functions). Maybe build you own RAII wrapper. – JHBonarius Mar 17 '22 at 09:27
-
1Note that even if you can get the `FILE*` from the stream the stream may have buffered data which is not reflected in the `FILE*` and modifying the `FILE*` may put the stream into an unexpected state – Alan Birtles Mar 17 '22 at 09:39
-
You cannot do this, read this: https://stackoverflow.com/questions/109449/getting-a-file-from-a-stdfstream. – Jabberwocky Mar 17 '22 at 09:41
-
i understand that i can't directly do this, but my code is in c++ and the library is in C. I don't have access to the source code for that library and the exposed API is using FILE*. Any suggestions are welcome. – harbinger Mar 17 '22 at 09:56
-
The suggestion is to use C `fopen` to open the file and pass it to the C API, and not use an `istream` for that file. – BoP Mar 17 '22 at 11:41