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
32
votes
1 answer

how to format hex numbers using stringstream

I am trying to convert an unsigned short to its hexadecimal representation in uppercase and prefixed with 0's using stringstream. I can't seem to get the uppercase and 0's correct. here is what I have now: USHORT id = 1127; std::stringstream ss; ss…
mtijn
  • 3,610
  • 2
  • 33
  • 55
31
votes
4 answers

cout << stringstream

When I put something into a stringstream, let's say a real number, if I then insert that stringstream object into cout...what am I looking at? Usually I'm getting some strange number. Is this a memory location? Just curious. It looks like the…
MCP
  • 4,436
  • 12
  • 44
  • 53
29
votes
4 answers

C++: insert char to a string

so I am trying to insert the character, which i got from a string, to another string. Here I my actions: 1. I want to use simple: someString.insert(somePosition, myChar); 2. I got an error, because insert requires(in my case) char* or string 3. I…
user380041
28
votes
2 answers

how copy from one stringstream object to another in C++?

I have std::stringstream object ss1. Now, I would like to create another copy from this one. I try this: std::stringstream ss2 = ss1; or: std::stringstream ss2(ss1) neither works The error message is like this: std::ios::basic_ios(const std::ios…
skydoor
  • 25,218
  • 52
  • 147
  • 201
27
votes
5 answers

Copy data from fstream to stringstream with no buffer?

Is there anyway I can transfer data from an fstream (a file) to a stringstream (a stream in the memory)? Currently, I'm using a buffer, but this requires double the memory, because you need to copy the data to a buffer, then copy the buffer to the…
Brad
  • 10,015
  • 17
  • 54
  • 77
26
votes
1 answer

Detect when at the end of a stringstream

I am trying to write a function which will detect when I'm almost at the end of a stringstream, but it doesn't seem to work. Here's some code: std::string path(1.2.3); int number; std::stringstream ss(path); while (!ss.eof()) { if (ss.peek() !=…
JNevens
  • 11,202
  • 9
  • 46
  • 72
25
votes
4 answers

Is it possible to pass a stringstream as a function parameter?

Is it possible to pass in a stringstream and have the function write to it directly? I remember I saw a function invoked similar to something like this: my_func(ss << "text" << hex << 33);
rsk82
  • 28,217
  • 50
  • 150
  • 240
24
votes
6 answers

Center text in fixed-width field with stream manipulators in C++

I am refactoring some legacy code which is using printf with longs strings (without any actual formatting) to print out plain text table headers which looks notionally like this: | Table | Column | Header | which are currently being produced…
Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
24
votes
3 answers

Why are move semantics for a class containing a std::stringstream causing compiler errors?

How can I make this simple class movable? What I thought was correct just produces a wall of errors... #include #include #include class message { public: message() = default; // Move constructor …
x-x
  • 7,287
  • 9
  • 51
  • 78
22
votes
6 answers

C++ convert simple values to string

Right now I use the following piece of code to dummily convert basic types (int, long, char[], this kind of stuff) to std::string for further processing: template constexpr std::string stringify(const T& t) { std::stringstream ss; …
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
22
votes
6 answers

how to reuse stringstream

These threads do NOT answer me: resetting a stringstream How do you clear a stringstream variable? std::ifstream file( szFIleName_p ); if( !file ) return false; // create a string stream for parsing std::stringstream szBuffer; std::string szLine;…
Icebone1000
  • 1,231
  • 4
  • 13
  • 25
20
votes
2 answers

Is there a way to create a stringstream from a string_view without copying data?

I think that's a pretty straighforward question. I'd specifically like to use std::get_time, but it requires some sort of a stream to be used with. I am passing the data in a string_view and would like to avoid copying it just to parse the date.
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
20
votes
2 answers

Setting the content of a std::stringstream with str(const char*) has strange consequences

I use std::stringstream extensively to generate code. I have run into some querious behavior when setting the content with the str() function followed by using the operator<<. Can someone explain this behavior to me. I would very much appreciate it…
Troels Blum
  • 823
  • 8
  • 17
19
votes
2 answers

C++: vector to stringstream

I want to know if it is possible to transform a std::vector to a std::stringstream using generic programming and how can one accomplish such a thing?
Alerty
  • 5,945
  • 7
  • 38
  • 62
18
votes
4 answers

Reading getline from cin into a stringstream (C++)

So I'm trying to read input like this from the standard input (using cin): Adam English 85 Charlie Math 76 Erica History 82 Richard Science 90 My goal is to eventually store each data piece in its own cell in a data structure I…
user313
  • 681
  • 1
  • 8
  • 21
1 2
3
78 79