c++ - Reading contents from a stringstream -
i testing how read data out of std::streamstring
i'm getting wrong, point out what's problem? , give correct way read it?
my testing code is:
#include <iostream> #include <string> #include <sstream> #define buffer_size 16 int main(int argc, char ** argv) { std::stringstream ss; ss << "un texto con datos de ejemplo para probar la extacción de un stream"; std::cout << "stream contents: '" << ss.str() << "'" << std::endl; char buffer[buffer_size] = {0}; std::streamsize read = 0; { read = ss.readsome(buffer, buffer_size - 1); std::cout << "read: " << ss.gcount() << std::endl; std::cout << buffer << std::endl; std::cout << "---" << std::endl; std::fill(buffer, buffer + buffer_size, 0); } while ( read > 0 ); return 0; }
and i'm getting output:
stream contents: 'un texto con datos de ejemplo para probar la extacci¾n de un stream' read: 15 un texto con da --- read: 15 tos de ejemplo --- read: 15 para probar la --- read: 15 extacci¾n de un --- read: 5 stre --- read: 0 ---
as may notice last read operation reads 5 characters leaving out last 2 'am' though should have been able read it. missing somtehing?
actually, readsome
read available characters , depends on platform, allowed return 0 while there still characters in stream , subsequent call return missing characters. available data, i'd rather use read
in combination gcount
number of characters read.
do { ss.read(buffer, buffer_size - 1); read=ss.gcount(); std::cout << "read: " << ss.gcount() << std::endl; std::cout << buffer << std::endl; std::cout << "---" << std::endl; std::fill(buffer, buffer + buffer_size, 0); } while ( read > 0 );
just clarification:
while believe, behavoir observed allowed standard, don't see why implementation should behave when input stream based on string literal (im not familiar implementation details of streams). check is, if read
corresponds rdbuf()->in_avail()
, if doesn't might indeed compiler bug.
Comments
Post a Comment