Converting ifstreams to strings?

This is a discussion on Converting ifstreams to strings? within the C++ Programming forums, part of the General Programming Boards category; I'm pretty sureI remember sstream being needed to do this, and while I CAN indeed sstream << ifstream, when I ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    106

    Converting ifstreams to strings?

    I'm pretty sureI remember sstream being needed to do this, and while I CAN indeed sstream << ifstream, when I string = sstream.str(), the resultant string only prints out the place in memory. Is there some other argument I need to pass into .str() to get readable output, or am I just doing this wrong?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Dunno.
    Code:
    #include <iostream>
    #include <fstream>
    #include <sstream>
    
    int main()
    {
       std::ifstream file(__FILE__);
       std::ostringstream oss;
       if ( oss << file.rdbuf() )
       {
          std::cout << oss.str();
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting strings to char
    By Furious5k in forum C++ Programming
    Replies: 13
    Last Post: 01-02-2009, 04:26 PM
  2. Converting strings to chars
    By Suchy in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2007, 04:17 AM
  3. converting a vector of strings into an array.
    By LightsOut06 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:14 PM
  4. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM
  5. converting strings to ints
    By HomerJ in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 06:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21