Questions tagged [stringstream]

stringstream provides an interface to manipulate strings as if they were input/output streams.

The objects of this class maintain internally a pointer to a stringbuf object that can be obtained/modified by calling member rdbuf. This streambuf-derived object controls a sequence of characters (string) that can be obtained/modified by calling member str.

1178 questions
8
votes
5 answers

redirect std::cout to a custom writer

I want to use this snippet from Mr-Edd's iostreams article to print std::clog somewhere. #include #include #include #include int main() { std::ostringstream oss; // Make clog use the buffer from oss …
Dustin Getz
  • 21,282
  • 15
  • 82
  • 131
8
votes
4 answers

Rounding off floats with ostringstream

I have an issue regarding conversion from float to c++ string using ostringstream. Here is my line: void doSomething(float t) { ostringstream stream; stream << t; cout << stream.str(); } when t has value -0.89999 it is round off to…
boom
  • 5,856
  • 24
  • 61
  • 96
8
votes
2 answers

std::string stream parse a number in binary format

I need to parse an std::string containing a number in binary format, such as: 0b01101101 I know that I can use the std::hex format specifier to parse numbers in the hexadecimal format. std::string number = "0xff"; number.erase(0,…
Nick
  • 10,309
  • 21
  • 97
  • 201
8
votes
4 answers

Is there a way to reduce ostringstream malloc/free's?

I am writing an embedded app. In some places, I use std::ostringstream a lot, since it is very convenient for my purposes. However, I just discovered that the performance hit is extreme since adding data to the stream results in a lot of calls to…
Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
8
votes
2 answers

std::stringstream with direct output buffer / string result access, avoiding copy?

Is there a canonical / public / free implementations variant of std::stringstream where I don't pay for a full string copy each time I call str()? (Possibly through providing a direct c_str() member in the osteam class?) I've found two questions…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
8
votes
2 answers

Skip whitespaces with getline

I'm making a program to make question forms. The questions are saved to a file, and I want to read them and store them in memory (I use a vector for this). My questions have the form: 1 TEXT What is your name? 2 CHOICE Are you ready for these…
JNevens
  • 11,202
  • 9
  • 46
  • 72
8
votes
1 answer

Discrepancy between istream's operator>> (double& val) between libc++ and libstdc++

With my recent upgrade to Mac OS X 10.9 the default standard C++ library changed from libstdc++ to libc++. Since then I observe unexpected behaviour of the stringstream operator>>(double) documented in the code example below. In summary the libc++…
Stephan Aiche
  • 81
  • 1
  • 3
8
votes
2 answers

Disable commas in cout?

In a project I am currently working on I link to a proprietary dynamic library. As soon as I run the library's initialize function, the behavior of logging and printing of numbers changes. Commas have been inserted at every third decimal. Ie. cout…
dinkelk
  • 2,676
  • 3
  • 26
  • 46
8
votes
1 answer

stringstream->rdbuf()->pubsetbuf is not setting the buffer

I am trying to modify a stringbuffer of a stringstream object without having to copy a string, using the method pubsetbuf, but it is not working. I am following the documentation in http://www.cplusplus.com/reference/iostream/streambuf/pubsetbuf/.…
8
votes
6 answers

How to force std::stringstream operator >> to read an entire string?

How to force std::stringstream operator >> to read an entire string instead of stopping at the first whitespace? I've got a template class that stores a value read from a text file: template class ValueContainer { protected: T…
Saulius Žemaitaitis
  • 2,956
  • 3
  • 29
  • 36
8
votes
0 answers

std::basic_stringstream won't compile with MSVC 10

I'm trying to get UTF-8 chars to co-exist with ANSI 8-bit chars. My strategy has been to represent utf-8 chars as unsigned char so that appropriate overloads of functions can be used for the two character types. e.g. namespace MyStuff { …
Michael J
  • 7,631
  • 2
  • 24
  • 30
7
votes
5 answers

Concatenate ints in an array?

As part of a homework assignment I need to concatenate certain values in an array in C++. So, for example if I have: int v[] = {0,1,2,3,4} I may need at some point to concatenate v[1] -> v[4] so that I get an int with the value 1234. I got it…
Nate
  • 925
  • 2
  • 12
  • 23
7
votes
3 answers

How to insert a string to the beginning of a stringstream

For example only and not the actual code: stringstream ss; ss << " world!"; string hello("Hello"); // insert hello to beginning of ss ?? Thanks for all the responses, I also found this code, which works: ostringstream& insert( ostringstream& oss,…
dlsou
  • 635
  • 2
  • 8
  • 18
7
votes
2 answers

using stringstream to print a rounded floating point number

i have floating point variables "lmin" and "lmax". i wish to display only 4 significant digits. i am currently using something i have found online of the form ... string textout; stringstream ss; ss << lmin; textout = ss.str(); output(-0.5, -0.875,…
drjrm3
  • 4,474
  • 10
  • 53
  • 91
7
votes
2 answers

Ignore values from stringstream (like %*f in sscanf)

I'm trying to get rid of old unsafe C functions, including sscanf(). Right now I'm using #include std::string str = "111 222.2 333 444.4 555"; std::stringstream sstr(str); int i, j, k; float dummy1, dummy2; sstr >> i >> dummy1 >> j >>…
Gunnar
  • 213
  • 3
  • 13