Newbie question about streams (istream,ostream,ifstream)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kamikaze04
    New Member
    • Mar 2008
    • 3

    Newbie question about streams (istream,ostream,ifstream)

    Hello.

    I have a very newbie question about Streams. The situation is that i have a function (that i cannot modify it's definition/call):

    Code:
    public void F1(istream & in){
       while( ...) {
          in >> value
          do stuff with value;
       }
    }
    Until now this function was called passing a istream that had been previously filled with the content of a file (with ifstream).

    But now, i don't have a file to fill "in", i have a vector of strings like:

    Code:
    std:: vector <std::string> str_Vector;
    str_Vector.push_back("textA\n");
    str_Vector.push_back("textB\n");
    str_Vector.push_back("textC\n");
    So, i was wondering how to fill the variable "in" with the content of this vector like if it was read by a file containing:
    textA
    textB
    textC

    I have tried defining a "ostream" variable and also with a "ostringstr eam" variable, but it complains because the function F1 is waiting for a type "istream".

    I know that it seems a dummy question, but i can say that i have been with this stupid thing for hours... :(

    Thank you very much in advance,
  • fual
    New Member
    • Feb 2008
    • 28

    #2
    You could try using a stringstream or an iostream, they both inherit from istream and ostream, as such you can push things into them and then get them out afterwards. It will also work in your function[CODE=cpp]#include <sstream>

    int main (void) {
    std::stringstre am ss;

    "for each" (vector) {
    ss << element;
    }

    F1(ss);
    }[/CODE]That is pseudo code, but hopefully it should give you the idea.

    Comment

    • kamikaze04
      New Member
      • Mar 2008
      • 3

      #3
      Absolutely YES! fual,

      I was trying stupidly to define it as std:ostringstre am ss instead of just defining it as stringstream which allows me to insert and take from it.

      Just for future searchers, i just give here the code it is working now: (code can be simplified but i think it is usefull for newbies in this way)

      Code:
      static bool myfunction (istream & in) {
      
      string line;
      uint i;
      std::stringstream miout;
      
      std:: vector <std::string> str_Vector;
      str_Vector= file1(); //here i fill the vector with strings
      
      for (int aux=0 ; aux<str_Vector.size() ; aux++){
         line = str_Vector[aux];
         miout<<line<<endl;
      }
      
      bool ret = Pre_processor (miout);
      return ret;
      }
      Thanks again Fual.

      Comment

      • fual
        New Member
        • Feb 2008
        • 28

        #4
        No problem. Stringstreams are one of the most useful objects in the std library :) It took me a while to work that one out myself. Glad to be of assistance.

        Comment

        • fual
          New Member
          • Feb 2008
          • 28

          #5
          Oh while I am on really useful things ... With vectors NEVER[CODE=cpp]for( int i = 0; i != my_vector.size( ); ++i ) { ... }[/CODE]and ALWAYS[CODE=cpp]for( std::vector<my_ type>::iterator it = my_vector.begin ( ); it != my_vector.end( ); ++it ) { ... }[/CODE]You need a REALLY good reason to break that rule ;) So your code becomes[CODE=cpp]
          for ( std::vector<std ::string>::iter ator it = str_Vector.begi n( ); it != str_Vector.end( ); ++it ) {
          miout << *it << endl;
          }[/CODE]

          Comment

          • kamikaze04
            New Member
            • Mar 2008
            • 3

            #6
            Thank you about your enhacement in my code, i will test it tomorrow and tell you...but yes, it seems i'm a really newbie yet :(

            Thanks you again :)

            Comment

            Working...