iostream

C++ cin.fail() question

爱⌒轻易说出口 提交于 2019-11-27 07:24:01
问题 When running the following code and enter a number, it works fine. But when entering a letter, the program enters an infinite loop, displaying "Enter a number (0 to exit): cin failed." My intent was to handle the cin fail case and prompt the user again. int number; do{ cout << "Enter a number (0 to exit): "; cin >> number; if(cin.fail()){ cout << "cin failed." << endl; cin.clear(); }else{ cout << "cin succeeded, " << number << " entered." << endl; } }while(number != 0); 回答1: You need to clear

Why can't std::ostream be moved?

雨燕双飞 提交于 2019-11-27 07:22:29
Clearly, streams can't be copied. It should be possible to move streams. According to 27.9.1.11 [ofstream.cons] paragraph 4 it is possible to move construct an std::ofstream (the same is true for std::ifstream , std::fstream , and the std::*stringstream variants). For example: #include <iostream> #include <fstream> #include <string> std::ofstream makeStream(std::string const& name) { return std::ofstream(name); } int main() { std::ofstream out{ makeStream("example.log") }; } Trying to move an std::ostream , e.g., to have a factory function creating an std::ofstream , an std::ostringstream , or

Printing double without losing precision

坚强是说给别人听的谎言 提交于 2019-11-27 07:14:08
How do you print a double to a stream so that when it is read in you don't lose precision? I tried: std::stringstream ss; double v = 0.1 * 0.1; ss << std::setprecision(std::numeric_limits<T>::digits10) << v << " "; double u; ss >> u; std::cout << "precision " << ((u == v) ? "retained" : "lost") << std::endl; This did not work as I expected. But I can increase precision (which surprised me as I thought that digits10 was the maximum required). ss << std::setprecision(std::numeric_limits<T>::digits10 + 2) << v << " "; // ^^^^^^ +2 It has to do with the number of significant digits and the first

Overloading the QDataStream << and >> operators for a user-defined type

十年热恋 提交于 2019-11-27 06:56:45
问题 I have a an object I'd like to be able to read and write to/from a QDataStream. The header is as follows: class Compound { public: Compound(QString, QPixmap*, Ui::MainWindow*); void saveCurrentInfo(); void restoreSavedInfo(QGraphicsScene*); void setImage(QPixmap*); QString getName(); private: QString name, homeNotes, addNotes, expText; Ui::MainWindow *gui; QPixmap *image; struct NMRdata { QString hnmrText, cnmrText, hn_nmrText, hn_nmrNucl, notes; int hnmrFreqIndex, cnmrFreqIndex, hn

Why does writing to temporary stream fail?

烂漫一生 提交于 2019-11-27 06:54:19
问题 Consider the following code: #include <sstream> #include <iostream> class Foo : public std::stringstream { public: ~Foo() { std::cout << str(); } }; int main() { Foo foo; foo << "Test1" << std::endl; Foo() << "Test2" << std::endl; return 0; } When I execute this, it gives me: 004177FC Test1 I do not understand why the second example gives me gibberish. The temporary should live until the entire expression is evaluated, so why does it not behave the same as the first example? 回答1: I tested it.

Are there any tricks to use std::cin to initialize a const variable?

淺唱寂寞╮ 提交于 2019-11-27 06:52:55
Common std::cin usage int X; cin >> X; The main disadvantage of this is that X cannot be const . It can easily introduce bugs; and I am looking for some trick to be able to create a const value, and write to it just once. The naive solution // Naive int X_temp; cin >> X_temp; const int X = X_temp; You could obviously improve it by changing X to const& ; still, the original variable can be modified. I'm looking for a short and clever solution of how to do this. I am sure I am not the only one who will benefit from a good answer to this question. // EDIT: I'd like the solution to be easily

Obtain a std::ostream either from std::cout or std::ofstream(file)

北战南征 提交于 2019-11-27 06:37:11
how do I bind a std::ostream to either std::cout or to an std::ofstream object, depending on a certain program condition? Although this invalid for many reasons, I would like to achieve something that is semantically equivalent to the following: std::ostream out = condition ? &std::cout : std::ofstream(filename); I've seen some examples that are not exception-safe, such as one from http://www2.roguewave.com/support/docs/sourcepro/edition9/html/stdlibug/34-2.html : int main(int argc, char *argv[]) { std::ostream* fp; //1 if (argc > 1) fp = new std::ofstream(argv[1]); //2 else fp = &std::cout /

Skipping expected characters like scanf() with cin

坚强是说给别人听的谎言 提交于 2019-11-27 06:16:07
问题 How to achieve scanf("%d # %d",&a,&b); sort of effect with cin in C++ ? 回答1: You could create your own stream manipulator. It is fairly easy. #include <ios> #include <iostream> using namespace std; // skips the number of characters equal to the length of given text // does not check whether the skipped characters are the same as it struct skip { const char * text; skip(const char * text) : text(text) {} }; std::istream & operator >> (std::istream & stream, const skip & x) { ios_base::fmtflags

What exactly is streambuf? How do I use it?

泪湿孤枕 提交于 2019-11-27 06:13:39
I'm trying to learn a bit more about how I/O streams work in C++, and I'm really confused at when to use what. What exactly is a streambuf ? When do I use a streambuf , as compared to a string , an istream , or a vector ? (I already know the last three, but not how streambuf compares to them, if it does at all.) bames53 Stream buffers represent input or output devices and provide a low level interface for unformatted I/O to that device. Streams, on the other hand, provide a higher level wrapper around the buffer by way of basic unformatted I/O functions and especially via formatted I/O

bidirectional iterator over file/ifstream

匆匆过客 提交于 2019-11-27 05:57:43
问题 I need an input file stream which would have a bidirectional iterator/adapter. Unfortunately std::ifstream (and similar) can be used only with std::istream_iterator which is a kind of forward iterator which cannot go backwards. (or am I mistaken here?) I could simply load the whole file to memory and then use a much more powerful random-access iterator over the array; however I would like to avoid that, and read only as much as I really need. It may happen that I really need only a small