stringstream

How to read file content into istringstream?

In order to improve performance reading from a file, I'm trying to read the entire content of a big (several MB) file into memory and then use a istringstream to access the information. My question is, which is the best way to read this information and "import it" into the string stream? A problem with this approach (see bellow) is that...

What is a "query parameter" in C++?

Hi, We were using stringstream to prepare select queries in C++. But we were strongly advised to use QUERY PARAMETERS to submit db2 sql queries to avoid using of stringstream. Can anyone share what exactly meant by query parameter in C++? Also, share some practical sample code snippets. Appreciate the help in advance. Edit: It is stri...

Writing stringstream contents into ofstream

I'm currently using a std::ofstream a function and a std::stringstream std::ofstream outFile; outFile.open(output_file); Then I call a function GetHolesResults(..., std::ofstream &outFile){ float x = 1234; std::stringstream ss; ss << x << std::endl; outFile << ss; } Now my outFile contains nothing but garbage "0012E708" re...

what is the difference between stringstream clear and str

I just wanted to know what's the difference between clear() and str(""); For example: stringstream ss("Stack Overflow"); ss.clear(); ss.str(""); I wanted to know the underlying technical difference. ...

How to deal with sstream vs. strstream inconsistency from old compilers

I'm temporarily using gcc 2.95.2, and instead of having a sstream header, it defines a (slightly different, and deprecated) strstream. I'm currently getting around this with #if __GNUC__ < 3 // or whatever version number it changes #include <strstream> #else #include <sstream> #endif and then things like: #if __GNUC__ < 3 s...

C++, Going from string to stringstream to vector<int>

Hi, I've this sample program of a step that I want to implement on my application. I want to push_back the int elements on the string separately, into a vector. How can I? #include <iostream> #include <sstream> #include <vector> using namespace std; int main(){ string line = "1 2 3 4 5"; //includes spaces stringstream lineS...

Standard behavior of tellp on empty ostringstream

I have a question on the standard behavior of calling tellp on an empty ostringstream. I have a function foo which calls tellp the first thing: void foo(std::ostream& os) { std::ostream::pos_type pos = os.tellp(); // do some stuff. } int main() { std::ostringstream os; foo(os); } In Visual Studio 2005, calling this fu...

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 <iostream> #include <iomanip> #include <string> #include <sstream> int main() { std::ostringstream oss; // Make clog use the buffer from oss std::streambuf *former_buff = std::clog.rdbuf(oss.rdbuf()); std::clog <<...

Good tutorial for stringstream manipulation in C++

I'm looking for a good tutorial on how to do string manipulation with stringstream in C++. Currently, I'm doing the following: double d = 7.234; stringstream out; out.width(8); out.precision(6); out << fixed << d; I like this link for a list of what options are available, but I learn best from examples and would like to see some more...

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? ...

Can I pass C++ strings into a method in the style of a stream?

I'd like to do this: MyClass mc = MyClass("Some string" << anotherString); Thanks for your answers, I have decided to re-write this question based on what you've told me, as it's gotten a little messy. Eventually, I read C++ format macro / inline ostringstream, and decided to use a macro, as it's not really possible to do this using a...

Why is stringstreams rdbuf() and str() giving me different output?

I have this code, int main() { std::string st; std::stringstream ss; ss<<"hej hej med dig"<<std::endl; std::getline(ss,st,' '); std::cout <<"ss.rdbuf()->str() : " << ss.rdbuf()->str(); std::cout <<"ss.rdbuf() : " << ss.rdbuf(); return 0; } Giving me this output ss.rdbuf()->str() : hej hej med dig ...

How to Parse Lines With Differing Number of Fields in C++

I have a data that looks like this: AAA 0.3 1.00 foo chr1,100 AAC 0.1 2.00 bar chr2,33 AAT 3.3 2.11 chr3,45 AAG 1.3 3.11 qux chr1,88 ACA 2.3 1.33 chr8,13 ACT 2.3 7.00 bux chr5,122 Note that the lines above are tab separated. Moreover, it sometime may contain 5 fields or 4 fields. What I want to do is to capture 4th fields in...

stringstream unsigned conversion broken?

Consider this program: #include <iostream> #include <string> #include <sstream> #include <cassert> int main() { std::istringstream stream( "-1" ); unsigned short n = 0; stream >> n; assert( stream.fail() && n == 0 ); std::cout << "can't convert -1 to unsigned short" << std::endl; return 0; } I tried this on gc...

Working with string streams?

Say i have a stringsteam in C++, and I want to do different operations to it like: Searching for a sequence of characters, Converting block of text into int (in the middle of the line), Moving the get pointer back and forth and so on. What is the standard/common way of doing this kind of things with stringstreams? ...

Best way to empty stringstream?

One of the possibilities is: somestringstream.str(""); But is it most optimal? Is there any way to preserve stringstream internal buffer, so that following operator<<() calls would not require to reserve memory again? ...

stringstream bug in VC9? "Cannot access private member"

std::string str; std::stringstream strm(str); I get this error: Error 11 error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>' c:\program files\microsoft visual studio 9.0\vc\include\sstream 517 If I use istringstream, same happens...

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 <typename T> class ValueContainer { protected: T m_value; public: /* ... */ virtual void fromString(std::string & str) { std::strings...

get list of numbers from stdin and tokenize them

How would I get a list of numbers from the user and then tokenize them. This is what I have but it doesn't get anything except for the first number: #include <iostream> #include <sstream> #include <vector> #include <string> using namespace std; int main() { string line = ""; cin >> line; stringstream lineStream(line); ...

stringstream extraction not working

I seem to be having a problem with extracting data from a stringstream. The start of my extraction seems to be missing the first two characters. I have something similar to the following code: std::stringstream ss( std::stringstream::in | std::stringstream::out ); bool bValid; double dValue; double dTime; for( ...