Thread: Can you convert a FILE* stream to std::istream?

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    Can you convert a FILE* stream to std::istream?

    As far as I know, there's no C++ equivalent for popen() and many other C functions, and I like using C++ as much as possible... Is there any way to convert a FILE* stream to a C++ stream like std::istream so I can use things like std::getline()...?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Behind the scenes there is probably a FILE * somewhere inside the istream. But you probably can't get to that in a public way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course, STL is implementation defined, but in MSVC, C++ I/O is just a wrapper for C I/O, but you can't access the whole "C," so in either case you can't do that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Many standard libraries just wrap C I/O, but this is in no way required. And very few provide a way of getting at the FILE*.

    There are some alternatives. You could write your own stream buffer to wrap FILE*s. Or you could use the (not wholly complete) Boost/GSoC2006 library Boost.Process.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Damn... OK, I guess I'll just have to write my own getline( FILE*, std::string& ) function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Closing a stream
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2008, 05:15 PM
  3. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. Replies: 3
    Last Post: 04-04-2002, 05:27 PM