how to copy iostream& data into string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hanna88
    New Member
    • Oct 2009
    • 33

    how to copy iostream& data into string?

    hello,
    im having problem to copy the data from iostream& into string.and im not sure am i doing the right way by using getline.can anyone give hints how to solve this.

    here's to make things clear:


    Code:
    //method that reads through the stream and breaks it into words and sentence
    
    Document*Repository::addDoc(iostream& inStream,string docId)
    
    //test core
    
    Repository r1;
    stringstream ss;
    
    string s1 = "Hello World;"
    
    ss.clear();
    ss<<s1;
    r1.addDoc(ss,"1")
    thanks in advance
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    That code snippet does access iostream& and doesn't copy into a string.

    It copies a string into a streamstream which looks ok to me.

    Comment

    • hanna88
      New Member
      • Oct 2009
      • 33

      #3
      thanks for your reply.

      Originally posted by Banfa
      That code snippet does access iostream& and doesn't copy into a string.

      It copies a string into a streamstream which looks ok to me.
      so how can i access the copied string into streamstream should i convert it back to string in order to use string method like strtok?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        If you mean should you convert it into a C string to use strtok then no. strtok is an evil function and should be avoided.

        However you could convert it back into a string (the C++ variety) and use the member functions of string (find and substr) to tokenise it.

        Comment

        • hanna88
          New Member
          • Oct 2009
          • 33

          #5
          the string is been passed in iostream& form and the only for me to get access on the string is getline().im not sure whether it is right by using getline() first then convert it into string in order to tokenize it.

          any hint would be great! thanks again

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            You don't need to use getline then convert it to a string, getline can read straight into a string

            string getline reference

            Even so conversion to a string is still better than calling strtok.

            Comment

            Working...