dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Using std::istream with an already existing file

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    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

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    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

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,924

    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++17 Compiler: Microsoft VS2019 (16.4.0)

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,573

    Re: Using std::istream with an already existing file

    Quote Originally Posted by 2kaud View Post
    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*
    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

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,716

    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

  6. #6
    Join Date
    Nov 2003
    Posts
    1,902

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)