STL to: Cin, Cout, ifstream & ofstream

This is a discussion on STL to: Cin, Cout, ifstream & ofstream within the C++ Programming forums, part of the General Programming Boards category; Hello. I am very close to getting my program working. I would like to know how to send something from ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    STL to: Cin, Cout, ifstream & ofstream

    Hello.
    I am very close to getting my program working. I would like to know how to send something from an STL container to:

    1) cin
    2) cout

    and especially

    3) ifstream
    4) ofstream

    ofstream temp;
    std::string text;

    temp << text // this does not work.

    It seems I cannot use the sample approach with STL as character array or string.

    Do I need to implement an iterator?

    Kuphryn
    Last edited by kuphryn; 12-01-2001 at 09:47 PM.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You can't output to an input stream, thats why

    ifstream temp;
    std::string text;

    temp << text; // this does not work.
    temp >> text; // works

    You can define << and >> operators for streams and STL container classes if you want to.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    oh. Sorry about that. I meant "ofstream."

    Kuphryn

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    std::string already has the << and >> operators defined, so there's no reason that temp << text shouldn't work.

    For the other STL containers you can access the elements using the [] operator if one is defined or using the relevant iterator(you don't have to implement you own), and send them to an output stream if the object type in the container has the << operator defined.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  2. problems with ifstream and ofstream!! help me please!!!
    By Leeman_s in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2002, 01:51 PM
  3. ifstream, ofstream
    By ssjnamek in forum C++ Programming
    Replies: 7
    Last Post: 02-04-2002, 01:20 PM
  4. Ofstream and Ifstream
    By unanimous in forum C++ Programming
    Replies: 1
    Last Post: 12-31-2001, 10:44 AM
  5. having trouble with ifstream
    By Shy_girl_311 in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2001, 03:42 AM

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