iostream

C++ Standard Library: How to write wrappers for cout, cerr, cin and endl?

岁酱吖の 提交于 2019-11-26 00:26:13
问题 I do not like using namespace std , but I am also tired of having to type std:: in front of every cout , cin , cerr and endl . So, I thought of giving them shorter new names like this: // STLWrapper.h #include <iostream> #include <string> extern std::ostream& Cout; extern std::ostream& Cerr; extern std::istream& Cin; extern std::string& Endl; // STLWrapper.cpp #include \"STLWrapper.h\" std::ostream& Cout = std::cout; std::ostream& Cerr = std::cerr; std::istream& Cerr = std::cin; std::string

Testing stream.good() or !stream.eof() reads last line twice [duplicate]

北城余情 提交于 2019-11-26 00:19:39
问题 Possible Duplicate: Why is iostream::eof inside a loop condition considered wrong? I have the following piece of code: ifstream f(\"x.txt\"); string line; while (f.good()) { getline(f, line); // Use line here. } But this reads the last line twice. Why does this happen and how do I fix it? Something very similar happens with: ifstream f(\"x.txt\"); string line; while (!f.eof()) { getline(f, line); // Use line here. } 回答1: You very, very rarely want to check bad, eof, and good. In particular

Why is reading lines from stdin much slower in C++ than Python?

蹲街弑〆低调 提交于 2019-11-25 23:01:44
问题 I wanted to compare reading lines of string input from stdin using Python and C++ and was shocked to see my C++ code run an order of magnitude slower than the equivalent Python code. Since my C++ is rusty and I\'m not yet an expert Pythonista, please tell me if I\'m doing something wrong or if I\'m misunderstanding something. (TLDR answer: include the statement: cin.sync_with_stdio(false) or just use fgets instead. TLDR results: scroll all the way down to the bottom of my question and look at

Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?

别等时光非礼了梦想. 提交于 2019-11-25 22:50:33
问题 I just found a comment in this answer saying that using iostream::eof in a loop condition is \"almost certainly wrong\". I generally use something like while(cin>>n) - which I guess implicitly checks for EOF. Why is checking for eof explicitly using while (!cin.eof()) wrong? How is it different from using scanf(\"...\",...)!=EOF in C (which I often use with no problems)? 回答1: Because iostream::eof will only return true after reading the end of the stream. It does not indicate, that the next

No matching function - ifstream open()

断了今生、忘了曾经 提交于 2019-11-25 22:49:21
This is the part of the code with an error: std::vector<int> loadNumbersFromFile(std::string name) { std::vector<int> numbers; std::ifstream file; file.open(name); // the error is here if(!file) { std::cout << "\nError\n\n"; exit(EXIT_FAILURE); } int current; while(file >> current) { numbers.push_back(current); file.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } return numbers; } And well, I kind of have no idea what is going on. The whole thing compiles properly in VS. However I need to compile this with dev cpp. I commented out the line throwing errors in the code above. The

How to properly overload the << operator for an ostream?

让人想犯罪 __ 提交于 2019-11-25 22:47:11
问题 I am writing a small matrix library in C++ for matrix operations. However my compiler complains, where before it did not. This code was left on a shelf for 6 months and in between I upgraded my computer from debian etch to lenny (g++ (Debian 4.3.2-1.1) 4.3.2 ) however I have the same problem on a Ubuntu system with the same g++. Here is the relevant part of my matrix class: namespace Math { class Matrix { public: [...] friend std::ostream& operator<< (std::ostream& stream, const Matrix&

Reading from text file until EOF repeats last line [duplicate]

让人想犯罪 __ 提交于 2019-11-25 22:35:21
问题 This question already has an answer here: Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong? 4 answers The following C++ code uses a ifstream object to read integers from a text file (which has one number per line) until it hits EOF . Why does it read the integer on the last line twice? How to fix this? Code: #include <iostream> #include <fstream> using namespace std; int main() { ifstream iFile(\"input.txt\"); // input.txt has integers, one per line

Why would we call cin.clear() and cin.ignore() after reading input?

你离开我真会死。 提交于 2019-11-25 22:25:49
问题 Google Code University\'s C++ tutorial used to have this code: // Description: Illustrate the use of cin to get input // and how to recover from errors. #include <iostream> using namespace std; int main() { int input_var = 0; // Enter the do while loop and stay there until either // a non-numeric is entered, or -1 is entered. Note that // cin will accept any integer, 4, 40, 400, etc. do { cout << \"Enter a number (-1 = quit): \"; // The following line accepts input from the keyboard into //

Output unicode strings in Windows console app

我的梦境 提交于 2019-11-25 22:20:53
问题 Hi I was trying to output unicode string to a console with iostreams and failed. I found this: Using unicode font in c++ console app and this snippet works. SetConsoleOutputCP(CP_UTF8); wchar_t s[] = L\"èéøÞǽлљΣæča\"; int bufferSize = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, NULL, NULL); char* m = new char[bufferSize]; WideCharToMultiByte(CP_UTF8, 0, s, -1, m, bufferSize, NULL, NULL); wprintf(L\"%S\", m); However, I did not find any way to output unicode correctly with iostreams. Any

Why does reading a record struct fields from std::istream fail, and how can I fix it?

自古美人都是妖i 提交于 2019-11-25 22:19:33
问题 Suppose we have the following situation: A record struct is declared as follows struct Person { unsigned int id; std::string name; uint8_t age; // ... }; Records are stored in a file using the following format: ID Forename Lastname Age ------------------------------ 1267867 John Smith 32 67545 Jane Doe 36 8677453 Gwyneth Miller 56 75543 J. Ross Unusual 23 ... The file should be read in to collect an arbitrary number of the Person records mentioned above: std::istream& ifs = std::ifstream(\