stringstream

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

六眼飞鱼酱① 提交于 2019-12-05 01:19:37
问题 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 calls. I thought width() did what I wanted, but I was apparently wrong... EDIT: I'm using the stream extraction operators to perform the extraction, since these

C++: From stringstream to char**

拟墨画扇 提交于 2019-12-04 21:32:28
I have a class with parse(int argc, char* argv[]) function which I have to use to set a desired state of an object. I'm taking the parameters from the gui using stringstream and then I'm trying to convert them to char** to pass them to the function. Here's what I've got: std::stringstream sstream; sstream << "-clip" << " " << min_x_entry.get_text() << " " << max_x_entry.get_text(); // etc. std::cout << sstream.str(); // All looks good here std::vector<std::string> args; std::vector<char*> argv; std::string arg; while (sstream >> arg) { args.push_back(arg); argv.push_back(const_cast<char*>(args

num_get facet and stringstream conversion to boolean - fails with initialised boolean?

风流意气都作罢 提交于 2019-12-04 17:20:41
I have inherited a template to convert a string to a numerical value, and want to apply it to convert to boolean . I am not very experienced with the stringstream and locale classes. I do seem to be getting some odd behaviour, and I am wondering if someone could please explain it to me? template<typename T> T convertFromString( const string& str ) const { std::stringstream SStream( str ); T num = 0; SStream >> num; return num; } This works fine until I try the boolean conversion string str1("1"); int val1 = convertFromString<int>(str1); // ok string str2("true"); bool val2 = convertFromString

c++ execute function any time a stream is written to

南笙酒味 提交于 2019-12-04 12:58:46
I have a simple GUI program that uses a custom stringstream to redirect output from the console to a text field in the GUI (under some circumstances). currently. the window redraws any time I hit enter, but it's possible that output could be generated at other times. Is there a way to register a function with the stringstream that gets executed every time the << operator is used on the stream? NOTE I should have pointed out that I cannot use C++11 in my solution. the machines on which this will be compiled and run will not have c++11 available. Personally, I wouldn't use an std::ostringstream

C++: what benefits do string streams offer?

最后都变了- 提交于 2019-12-04 06:22:24
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? You can use string streams to convert anything that implements operator << to a string: #include <sstream> template<typename T> std::string toString(const T& t) { std::ostringstream stream; stream << t; return stream.str(); } or even template <typename U, typename T> U convert(const T& t) { std::stringstream stream; stream << t; U u; stream >> u; return u; } I use them mostly as memory buffers, in creating

Can I tell if a std::string represents a number using stringstream?

倖福魔咒の 提交于 2019-12-04 03:36:12
Apparently this is suposed to work in showing if a string is numerical, for example "12.5" == yes, "abc" == no. However I get a no reguardless of the input. std::stringstream ss("2"); double d; ss >> d; if(ss.good()) {std::cout<<"number"<<std::endl;} else {std::cout<<"other"<<std::endl;} You should use an istringstream so that it knows it's trying to parse input. Also, just check the result of the extraction directly rather than using good later. #include <sstream> #include <iostream> int main() { std::istringstream ss("2"); double d = 0.0; if(ss >> d) {std::cout<<"number"<<std::endl;} else

how to read stringstream with dynamic size?

谁都会走 提交于 2019-12-04 02:38:40
I wanted to experiment with stringstream for an assignment, but I'm a little confused on how it works. I did a quick search but couldn't find anything that would answer my question. Say I have a stream with a dynamic size, how would I know when to stop writing to the variable? string var = "2 ++ asdf 3 * c"; stringstream ss; ss << var; while(ss){ ss >> var; cout << var << endl; } and my output would be: 2 ++ asdf 3 * c c I'm not sure why I get that extra 'c' at the end, especially since _M_in_cur = 0x1001000d7 "" You get the extra c at the end because you don't test whether the stream is still

What are the default Format Flags (and widths) for double output in a std::stringstream?

浪子不回头ぞ 提交于 2019-12-03 21:35:02
What is the default format, when I'm writting a double to a stringstream ? double v = 3.0; std::stringstream ss; ss << v; Where can I find a list of the default format setup for a stringstream ? Is the default format the same for all derived classes of std::istream (within the stdlib)? The defaults are setup by std::basic_ios::init and are the same for all streams derived from ios_base . The defaults are: rdbuf() sb tie() 0 rdstate() goodbit if sb is not a null pointer, otherwise badbit. exceptions() goodbit flags() skipws | dec width() 0 precision() 6 fill() widen(’ ’) getloc() a copy of the

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

房东的猫 提交于 2019-12-03 16:58:22
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 calls. I thought width() did what I wanted, but I was apparently wrong... EDIT: I'm using the stream extraction operators to perform the extraction, since these are flat text files with values separated by whitespace. char x[4]; cin.width(4); cin >> x; cout << x;

Why does using std::endl with ostringstream affect output speed?

。_饼干妹妹 提交于 2019-12-03 13:04:59
I'm timing the difference between various ways to print text to standard output. I'm testing cout , printf , and ostringstream using both \n and std::endl . I expected std::endl to make a difference with cout (and it did), but I didn't expect it to slow down output with ostringstream . I thought using std::endl would just write a \n to the stream and it would still only get flushed once. What's going on here? Here's all my code: // cout.cpp #include <iostream> using namespace std; int main() { for (int i = 0; i < 10000000; i++) { cout << "Hello World!\n"; } return 0; } // printf.cpp #include