iostream

Passing data from C++ to gnuplot example (using Gnuplot-iostream interface)

旧街凉风 提交于 2019-12-23 13:24:30
问题 I have just come accross Dan Stahlke's gnuplot C++ I/O interface, which saves me from "rolling my own". Unfortunately, there are not too may examples and there ios no real documentation. I have the following data types in my C++ project: struct Data { std::string datestr; // x axis value float f1; // y axis series 1 float f2; // y axis series 2 float f3; // y axis series 3 }; typedef std::vector<Data> Dataset; I want to pass a Dataset variable from C++, so that I can plot the data (dates on

cin erratic behaviour

我只是一个虾纸丫 提交于 2019-12-23 12:44:48
问题 I'm a newbie to C++. Small code sample follows: int main(int argc, char* argv[]) { char ch1; int int1; cin >> ch1; cin >> int1; cout << ch1 << '\n'; cout << int1 << '\n'; return 0; } When I run the program and input the following: az I get as output: a 32767 I understand the 'a' but why the integer value of 32767? I just want to test and see what happen if instead of a numeric value assigned to int1 i used a 'z'. I try inputting: ax ...and I also get same results. Now if instead of int int1 I

Output a nested class inside a template

梦想与她 提交于 2019-12-23 10:42:11
问题 I'm trying to overload the ostream operator to allow output for a nested class inside a template. However, the compiler is unable to bind the actual function call to my overload. template <class T> struct foo { struct bar { }; }; template <class T> std::ostream& operator << (std::ostream& os, const typename foo<T>::bar& b) { return os; } int main() { foo<int>::bar b; std::cout << b << std::endl; // fails to compile } This will compile if I define the overload as an inline friend function:

Can C++ streambuf methods throw exceptions?

社会主义新天地 提交于 2019-12-23 10:17:42
问题 I'm trying to find a method to get the number of characters read or written to a stream that is reliable even if there is an error and the read/write ends short. I was doing something like this: return stream.rdbuf()->sputn(buffer, buffer_size); but if the streambuf implementation of overflow is permitted to throw excpections this won't work. Is it? I've not been able to find it documented anywhere. 回答1: basic_streambuf::overflow is allowed to throw an exception upon failure as documented in

Performant way of formatting numbers according to the user's locale with iostream?

 ̄綄美尐妖づ 提交于 2019-12-23 03:39:35
问题 In one part of our application there is the requirement to format numbers according to the user's locale. The interface essentially looks like this: std::string format_number(double number); The old implementation looked like this: std::string format_number(double number) { using namespace std; static const locale user_loc(""); ostringstream fmtstream; fmtstream.imbue(user_loc); fmtstream << std::fixed; fmtstream << number; return fmtstream.str(); } We have now noticed that with our compiler

Getting last char sent to std::cout

限于喜欢 提交于 2019-12-23 03:15:21
问题 I need at the end of main() execution to check if last char sent to stdout (through std::cout ) was '\n' (or platform specific end-of-line). How to test for this? It is safe to assume that C style io (like printf) was not used. Program is REPL for C++. It evaluates C++ expressions (or statements) and prints results on stdout. It is desirable that output would be always terminated with single new-line. 回答1: It is similar answer to this given by @Kevin. However I believe it is better for your

Getting last char sent to std::cout

╄→гoц情女王★ 提交于 2019-12-23 03:15:09
问题 I need at the end of main() execution to check if last char sent to stdout (through std::cout ) was '\n' (or platform specific end-of-line). How to test for this? It is safe to assume that C style io (like printf) was not used. Program is REPL for C++. It evaluates C++ expressions (or statements) and prints results on stdout. It is desirable that output would be always terminated with single new-line. 回答1: It is similar answer to this given by @Kevin. However I believe it is better for your

Tell `endl` not to flush

江枫思渺然 提交于 2019-12-23 03:05:00
问题 My program prints a large number of short lines to cout . As a slightly contrived example, my lines look a little like this: cout<<"The variable's value is: "<<variable<<endl; I'd like the program to run fast and I do believe that endl is killing me because it initiates a buffer flush on cout every time it is used. Now, some folks on the internet have said that I could do this instead: cout<<"The variable's value is: "<<variable<<"\n"; But this does not seem like a good solution because endl

Combination of std::ios::openmode to avoid modifications of an existing file?

混江龙づ霸主 提交于 2019-12-23 03:03:58
问题 Is there an available combination of std::ios::openmode to avoid modifications of an existing file and allow only the creation of a new one ? 回答1: No there isn't. See the C++03 Standard § 27.4.2.1.4/1, or the C++11 Standard § 27.5.3.1.4/1 if to hand or otherwise http://en.cppreference.com/w/cpp/io/ios_base/openmode. As none of the specified constants that may be |-conjoined to form an openmode implies that the opened file shall not exist no conjunction of them can have that implication. 来源:

How to read content of .EXE file in Java

匆匆过客 提交于 2019-12-22 17:39:18
问题 What are the possible options and the most appropiate for reading an executable file in Java. I want produce the hexadecimal representation of an .exe file. Im thinking of reading the file in binary and then doing the conversion. But how can i read the .exe? 回答1: 1) read the file in as bytes. use BufferedInputStream( new FileInputStream( new File("bin.exe") ) ) 2) convert each byte to hex format. static final String HEXES = "0123456789ABCDEF"; public static String getHex( byte [] raw ) { if (