-
September 19th, 2015, 05:51 AM
#1
Using std::istream with an already existing file
Is it possible to attach an istream object to an already opened file handle or FILE* (so I could read the file contents into my object?)
I discovered recently that ifstream has a c'tor for this but it doesn't seem to be universal (it's available in some versions of MSVC but not for other compilers). The reason I need an already opened file is that I need to open it from a cross-platform file path (in UTF-8 format). So I can't rely on ifstream directly - nor open / fopen etc, as they don't accept UTF-8 paths on Windows. So for opening the files I use a 3rd party library which does understand UTF-8 (hope that makes sense).
P.S. I've just been reading about something called std::filebuf which apparently CAN connect to std::istream. I'm not sure if that would help at all? (I'd never heard of std::filebuf until a few minutes ago ). Is it even available in MSVC? I couldn't find much about it on MSDN...
Last edited by John E; September 19th, 2015 at 08:08 AM.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
September 19th, 2015, 10:47 AM
#2
Re: Using std::istream with an already existing file
After some further experiments I've realised that (in MSVC at least) std::filebuf does exist and it offers a c'tor of type filebuf::filebuf(FILE*). So if that's available outside of MSVC my problem's solved. Anyone know if that c'tor is part of the official standard (i.e. it's not just some MSVC-specific extension) ?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
September 19th, 2015, 11:28 AM
#3
Re: Using std::istream with an already existing file
The c++ standard for filebuf is described here http://www.cplusplus.com/reference/fstream/filebuf/
There is no mention of a c'tor taking a type of FILE*
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 24th, 2015, 11:29 AM
#4
Re: Using std::istream with an already existing file
Originally Posted by 2kaud
Thanks 2kaud. Sorry for the delay in replying.
I did some research into this and while you're right (i.e. a c'tor taking FILE* isn't officially part of the standard) it's often supported as a compiler extension (apparently) because it can be useful when handling pipes & sockets.
If there's anyone here using gcc, would they mind letting me know if the following code will compile with it..?
Code:
#include <stdio.h>
#include <iostream>
#include <fstream>
void some_function ()
{
FILE* fin = fopen ("whatever", "r");
std::filebuf fb (fin);
std::istream ifs (&fb);
}
Last edited by John E; September 24th, 2015 at 11:33 AM.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
September 24th, 2015, 04:15 PM
#5
Re: Using std::istream with an already existing file
It did not compile using g++ ver 4.8.1 under MinGW/Windows
Here is a link for converting a file descriptor to a ostream or istream with g++.
It compiled under g++ ver 4.8.1 under MinGW/Windows, but I did not test it.
https://gist.github.com/rajatkhanduja/2012695
-
September 24th, 2015, 08:07 PM
#6
Re: Using std::istream with an already existing file
If you don't want #define's for using GCC and MS extensions, then there's boost::filesystem as well (which become standardized).
gg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|