stringstream

[C++ stringstream] why this conversion doesn't work?

Below is my func. I call it with if(try_strtol(v, rhs)) and RHS = "15\t// comment" bool try_strtol(int64_t &v, const string& s) { try { std::stringstream ss(s); if ((ss >> v).fail() || !(ss >> std::ws).eof()) throw std::bad_cast(); return true; } catch(...) { return false; } } It returns false, i except true with v...

C++ 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 <string> #include <sstream> #include <iostream> using namespace std; int main() { stringstream ss("this is a ...

stringstream temporary ostream return problem

I'm creating a logger with the following sections: // #define LOG(x) // for release mode #define LOG(x) log(x) log(const string& str); log(const ostream& str); With the idea to do: LOG("Test"); LOG(string("Testing") + " 123"); stringstream s; LOG(s << "Testing" << 1 << "two" << 3); This all works as intended, but when I do: LOG(s...

Stream from std::string without making a copy?

I have a network client with a request method that takes a std::streambuf*. This method is implemented by boost::iostreams::copy-ing it to a custom std::streambuf-derived class that knows how to write the data to a network API, which works great. This means I can stream a file into the request without any need to read it all into memor...

std::stringstream GCC Abnormal Behavior

I have a very interesting problem with compiling a short little program on a Mac (GCC 4.2). The function below would only stream chars or strings into the stringstream, but not anything else (int, double, float, etc.) In fact, the fail flag is set if I attempt to convert for example an int into a string. However, removing the preprocess...

stringstream operator>> fails as function, but works as instance?

I'm writing simple code that will extract a bunch of name, int pairs from a file. I'm modifying existing code that just uses: string chrom; unsigned int size; while ( cin >> chrom >> size ) { // save values } But I want to use another (similar) input file that has the same first two columns, but are followed by other data (that w...

wistringstream from an xml file to an integer?

const XMLDataNode *pointsNode = node->GetChildren().at(0); std::wistringstream pointsstrm(*pointsNode->GetInnerText()); pointsstrm >> loadedGame.points; This is code I've written to pull an int from an XML file and pass it into loadedGame.points (an int). However, this isn't working. It compiles but doens't give the right value. Why...

Copying Part of Fstream to Istringstream

After a lot searching for solutions I've decided to actually ask for some help! I have a file that consists of a number of blocks, where each block may or may not be compressed. Before each block is an indication of the size of the block and whether the block is compressed. Within each block is a string that identifies the block. Giv...

redirected cout -> std::stringstream, not seeing EOL

Hi all, I've read a bunch of posts regarding redirecting std::cout to stringstreams, but I'm having problem reading the redirected string. std::stringstream redirectStream; std::cout.rdbuf( redirectStream.rdbuf() ); std::cout << "Hello1\n"; std::cout << "Hello2\n"; while(std::getline(redirectStream, str)) { // This does not work - ...

Should I preallocate std::stringstream?

I use std::stringstream extensively to construct strings and error messages in my application. The stringstreams are usually very short life automatic variables. Will such usage cause heap reallocation for every variable? Should I switch from temporary to class-member stringstream variable? In latter case how can I reserve stringstrea...

How to use C++ String Streams to append int ?

Hello, could anyone tell me or point me to a simple example of how to append an int to a stringstream containing the word "Something" (or any word)? Thanks! ...

Extract multiple words to one string variable

std::stringstream convertor("Tom Scott 25"); std::string name; int age; convertor >> name >> age; if(convertor.fail()) { // it fails of course } I'd like to extract two or more words to one string variable. So far I've read, it seems that it is not possible. If so, how else to do it? I'd like name to get all characters before ...

Using istringstream to process a memory block of variable length

I'm trying to use istringstream to recreate an encoded wstring from some memory. The memory is laid out as follows: 1 byte to indicate the start of the wstring encoding. Arbitrarily this is '!'. n bytes to store the character length of the string in text format, e.g. 0x31, 0x32, 0x33 would be "123", i.e. a 123-character string 1 byte s...

C++: what benefits do string streams offer?

could any one tell me about some practical examples on using string streams in c++, i.e. inputing and outputing to a string stream using stream insertion and stream extraction operators? ...

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 malloc and free. Is there any way to avoid it? My first thought was making the ostringstrea...

Setting the precision for stringstream globally

Hello all, I am using stringstream in my entire project which has more than 30 files. I recently overcomed an issue caused by stringstring where I was parsing the double to stringstream and there was a precision lost. So now I want to set the precision for all the files. Is there any way to set it somewhere globally so that I dont need ...

Turning temporary stringstream to c_str() in single statement

Consider the following function: void f(const char* str); Suppose I want to generate a string using stringstream and pass it to this function. If I want to do it in one statement, I might try: f((std::ostringstream() << "Value: " << 5).str().c_str()); // error This gives an error: 'str()' is not a member of 'basic_ostream'. OK, s...

How to set maximum read length for a stream in C++?

I'm reading data from a stream into a char array of a given length, and I'd like to make the maximum width of read to be large enough to fit in that char array. The reason I use a char array is that part of my specification is that the length of any individual token cannot exceed a certain value, so I'm saving myself some constructor c...

C++ Stringstream int to string but returns null

Hi below is my function: string Employee::get_print(void) { string out_string; stringstream ss; ss << e_id << " " << type << endl; out_string = ss.str(); return out_string; } e_id and type are int and they contain values from the class Employee. But when I pass them into the stringstream they just clear the string wh...

Capture data read from file into string stream Java

I'm coming from a C++ background, so be kind on my n00bish queries... I'd like to read data from an input file and store it in a stringstream. I can accomplish this in an easy way in C++ using stringstreams. I'm a bit lost trying to do the same in Java. Following is a crude code/way I've developed where I'm storing the data read line-b...