Thread: stringstream str() vs rdbuf()->pubsetbuf()

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55

    stringstream str() vs rdbuf()->pubsetbuf()

    hi,

    I've been toying with this & it doesn't seem to work.

    Code:
    	std::vector<char> vbuffer(filesize);
    	fHandle.read(&vbuffer[0],filesize);
    	stringstream ssbuffer;
    	ssbuffer.rdbuf()->pubsetbuf(&vbuffer[0],filesize); //<<issue.
    When I go to read from ssbuffer nothing is extracted & gcount() returns 0. Wondering if I'm missing something with this & how it works.

    So I've been tinkering with the following instead & wondering if I should expect any issues:
    Code:
    	std::vector<char> vbuffer(filesize);
    	fHandle.read(&vbuffer[0],filesize);
    
    	stringstream ssbuffer;
    	ssbuffer.str(&vbuffer[0]);

    Any feedback would be much appreciated,
    thanks

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The first example works fine for me
    Code:
    #include <string>
    #include <iostream>
    #include <sstream>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main() {
    
        vector<char> cv {'k','u','r','t' };
    
        stringstream str;
        str.rdbuf()->pubsetbuf(&cv[0], 3); // intentionally set the size to only 3
                                            // to see if it works if the string is not 0-terminated
        char c;
        str.get( c );
        string val;
        cout << "reading 1 char with get()\n"
             << "'" << c << "' gcount=" << str.gcount() << endl
             << "reading the rest returns \"";
        str >> val;
        cout << val << "\"" << endl
             << "now gcount() is still supposed to be 1 because it should return\nthe count of the last unformatted input gcount=" 
             << str.gcount() << endl;
    }
    my output :
    Code:
    reading 1 char with get()
    'k' gcount=1
    reading the rest returns "ur"
    now gcount() is still supposed to be 1 because it should return
    the count of the last unformatted input gcount=1
    The second example is the usual way to initialize the stringstream from a string.
    But I guess there is a problem the way you did it.
    Code:
    ssbuffer.str(&vbuffer[0]);
    you call str() with a string that is initialized from a const char *, but the data is possibly not 0-terminated.
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    hi

    thank you for the reply.
    Problem I'm having is once I do following;

    Code:
    			fHandle.open(lpszPathName, std::ios::out | std::ios::in | std::ios::binary);
    			std::stringstream ssbuffer;
    			std::vector<char> vbuffer(lfilesize);
    			fHandle.read(&vbuffer[0],lfilesize);
    			ssbuffer.rdbuf()->pubsetbuf(&vbuffer[0],lfilesize);
    Any read or get on the stream only returns the first 4-5 bytes. After that I don't get anything and size() or length() on stream::str() returns 0. I've realised I get a similar issue using the stream if I do;

    Code:
    		ssbuffer.str(&vbuffer[0]);
    File appears to be read in properly into the vector though.

    Code:
    			char ch;
    			CString str;
    			for(int i =0;i<100;i++)
    			{
    				ch = vbuffer.at(i); //<<no problem.
    				//ssbuffer.get(ch); //<<issue
    				str.Format(L"char: %d\n",ch);
    				//str.Format(L"char: %c\n"ch); //<<produces a String too long or IO Error
    				TRACE(str);
    			}

    File I'm reading in is in binary.
    I'd rather not have to use vector to do what I need, would prefer to use stream. Any help would be appreciated.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    this will initialize the stringstream with a string that stops at the first 0 character
    Code:
    ssbuffer.str(&vbuffer[0]);
    you have to use
    Code:
    ssbuffer.str(string(&vbuffer[0], vbuffer.size());
    Kurt

  5. #5
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    is there anyway to do this without copying?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stringstream help
    By Airick92 in forum C++ Programming
    Replies: 6
    Last Post: 04-02-2011, 10:28 AM
  2. stringstream Help...
    By neogst in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2011, 10:23 AM
  3. Replies: 2
    Last Post: 12-25-2008, 12:15 PM
  4. Weird problem using ifstream / rdbuf
    By McFury in forum C++ Programming
    Replies: 1
    Last Post: 12-01-2008, 07:42 AM
  5. stringstream
    By jk1998 in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2007, 06:35 PM