Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. stringstream reuse

stringstream reuse

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rob bakes
    wrote on last edited by
    #1

    Hello! Why doesn't this work? First time it works, second time it returns blanks. std::stringstream s1(std::stringstream::in | std::stringstream::out); s1<<"hello"; string x1; s1>>x1; //x1 contains hello. good s1<<"more"; string x2; s1>>x2; //nope. x2 contains blank Thanks -Rob

    M 1 Reply Last reply
    0
    • R rob bakes

      Hello! Why doesn't this work? First time it works, second time it returns blanks. std::stringstream s1(std::stringstream::in | std::stringstream::out); s1<<"hello"; string x1; s1>>x1; //x1 contains hello. good s1<<"more"; string x2; s1>>x2; //nope. x2 contains blank Thanks -Rob

      M Offline
      M Offline
      Mike Burston
      wrote on last edited by
      #2

      The code fails because the first 'read' operation on the stringstream results in 'eof'. The following line :

      string x1;
      s1>>x1; //x1 contains hello. good

      causes the stream to the 'drained', so 'eof' is set. The next 'write' to the stream does not reset the 'eof' bit, so when you try the next 'read' :

      string x2;
      s1>>x2;

      It fails because the inputbuffer of the stream has it's 'eof' bit set. I am not sure it is possible to work around this - as far as I know, it is not the intention of 'stringstream' to allow constant intermingling of 'reads' and 'writes' to the underlying string. I would think that once the stream has been 'drained', you need to either recreate or reinitialise the stream. ----------------------- The sermon on the mount... Man 1 : Hear that? Blessed are the greek. Man 2 : The greek? Man 1 : Well apparently, he's going to inherit the earth. Man 2 : Did anyone catch his name?

      R 1 Reply Last reply
      0
      • M Mike Burston

        The code fails because the first 'read' operation on the stringstream results in 'eof'. The following line :

        string x1;
        s1>>x1; //x1 contains hello. good

        causes the stream to the 'drained', so 'eof' is set. The next 'write' to the stream does not reset the 'eof' bit, so when you try the next 'read' :

        string x2;
        s1>>x2;

        It fails because the inputbuffer of the stream has it's 'eof' bit set. I am not sure it is possible to work around this - as far as I know, it is not the intention of 'stringstream' to allow constant intermingling of 'reads' and 'writes' to the underlying string. I would think that once the stream has been 'drained', you need to either recreate or reinitialise the stream. ----------------------- The sermon on the mount... Man 1 : Hear that? Blessed are the greek. Man 2 : The greek? Man 1 : Well apparently, he's going to inherit the earth. Man 2 : Did anyone catch his name?

        R Offline
        R Offline
        rob bakes
        wrote on last edited by
        #3

        Ah yes, thanks Mike , I added an s1.clear() and it works.

        M 1 Reply Last reply
        0
        • R rob bakes

          Ah yes, thanks Mike , I added an s1.clear() and it works.

          M Offline
          M Offline
          Mike Burston
          wrote on last edited by
          #4

          Must be getting sleepy - should have thought of 'clear()'!!! ----------------------- The sermon on the mount... Man 1 : Hear that? Blessed are the greek. Man 2 : The greek? Man 1 : Well apparently, he's going to inherit the earth. Man 2 : Did anyone catch his name?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups