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.

1089 questions
525
votes
8 answers

How do you clear a stringstream variable?

I've tried several things already, std::stringstream m; m.empty(); m.clear(); both of which don't work.
CodingWithoutComments
  • 33,560
  • 21
  • 70
  • 85
173
votes
8 answers

What's the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?

When would I use std::istringstream, std::ostringstream and std::stringstream and why shouldn't I just use std::stringstream in every scenario (are there any runtime performance issues?). Lastly, is there anything bad about this (instead of using a…
Oliver Baur
  • 1,915
  • 2
  • 14
  • 7
145
votes
2 answers

How to use stringstream to separate comma separated strings

I've got the following code: std::string str = "abc def,ghi"; std::stringstream ss(str); string token; while (ss >> token) { printf("%s\n", token.c_str()); } The output is: abc def,ghi So the stringstream::>> operator can separate strings…
B Faley
  • 14,391
  • 34
  • 113
  • 191
144
votes
5 answers

stringstream, string, and char* conversion confusion

My question can be boiled down to, where does the string returned from stringstream.str().c_str() live in memory, and why can't it be assigned to a const char*? This code example will explain it better than I can #include #include…
Graphics Noob
  • 9,132
  • 10
  • 40
  • 43
136
votes
4 answers

How do I convert from stringstream to string in C++?

How do I convert from std::stringstream to std::string in C++? Do I need to call a method on the string stream?
Nick Bolton
  • 34,516
  • 66
  • 162
  • 230
125
votes
1 answer

How to clear stringstream?

stringstream parser; parser << 5; short top = 0; parser >> top; parser.str(""); //HERE I'M RESETTING parser parser << 6; //DOESN'T PUT 6 INTO parser short bottom = 0; parser >> bottom; Why doesn't it work?
There is nothing we can do
  • 21,267
  • 27
  • 92
  • 184
123
votes
3 answers

Incomplete type is not allowed: stringstream

Why does this line give the error Error: incomplete type is not allowed? stringstream ss;
pighead10
  • 3,899
  • 10
  • 31
  • 51
102
votes
2 answers

Qt c++ aggregate 'std::stringstream ss' has incomplete type and cannot be defined

I have this function in my program that converts integers to strings: QString Stats_Manager::convertInt(int num) { stringstream ss; ss << num; return ss.str(); } But when ever i run this i get the…
tyty5949
  • 1,310
  • 5
  • 12
  • 16
93
votes
3 answers

resetting a stringstream

How do I "reset" the state of a stringstream to what it was when I created it? int firstValue = 1; int secondValue = 2; std::wstringstream ss; ss << "Hello: " << firstValue; std::wstring firstText(ss.str()); //print the value of firstText…
user974967
  • 2,638
  • 9
  • 26
  • 44
77
votes
4 answers

Why was std::strstream deprecated?

I recently discovered that std::strstream has been deprecated in favor of std::stringstream. It's been a while since I've used it, but it did what I needed to do at the time, so was surprised to hear of its deprecation. My question is why was this…
andand
  • 15,638
  • 9
  • 48
  • 76
67
votes
5 answers

Equivalent of %02d with std::stringstream?

I want to output an integer to a std::stringstream with the equivalent format of printf's %02d. Is there an easier way to achieve this than: std::stringstream stream; stream.setfill('0'); stream.setw(2); stream << value; Is it possible to stream…
Andreas Brinck
  • 47,252
  • 14
  • 79
  • 112
56
votes
3 answers

Why copying stringstream is not allowed?

int main() { std::stringstream s1("This is my string."); std::stringstream s2 = s1; // error, copying not allowed } I couldn't find a reason as to why i can't copy stringstream. could you provide some reference?
user756327
  • 591
  • 1
  • 4
  • 5
54
votes
4 answers

Writing stringstream contents into ofstream

I'm currently using std::ofstream as follows: std::ofstream outFile; outFile.open(output_file); Then I attempt to pass a std::stringstream object to outFile as follows: GetHolesResults(..., std::ofstream &outFile){ float x = 1234; …
Eric
  • 18,532
  • 17
  • 78
  • 138
54
votes
2 answers

Why does stringstream >> change value of target on failure?

From Stroustrup's TC++PL, 3rd Edition, Section 21.3.3: If we try to read into a variable v and the operation fails, the value of v should be unchanged (it is unchanged if v is one of the types handled by istream or ostream member functions). The…
user1823664
  • 1,021
  • 8
  • 15
53
votes
6 answers

StringStream in C#

I want to be able to build a string from a class that I create that derives from Stream. Specifically, I want to be able to write code like this: void Print(Stream stream) { // Some code that operates on a Stream. } void Main() { …
AndreyAkinshin
  • 17,047
  • 25
  • 91
  • 148
1
2 3
72 73