Tag: iostream

从文本文件读取,直到EOF重复上一行

以下C ++代码使用ifstream对象从文本文件(每行有一个数字)读取整数,直到它遇到EOF 。 为什么它读取最后一行的整数两次? 如何解决这个问题? 码: #include <iostream> #include <fstream> using namespace std; int main() { ifstream iFile("input.txt"); // input.txt has integers, one per line while (!iFile.eof()) { int x; iFile >> x; cerr << x << endl; } return 0; } input.txt : 10 20 30 输出 : 10 20 30 30 注意 :我跳过了所有错误检查代码,以保持代码片段小。 […]

为什么我们在读取input后调用cin.clear()和cin.ignore()?

谷歌代码大学的C ++教程曾经有这样的代码: // 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 […]

在Windows控制台应用程序中输出unicodestring

您好我试图输出unicodestring与控制台与iostreams和失败。 我发现这一点: 在c + +控制台应用程序中使用unicode字体和此代码段的作品。 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); 但是,我没有find任何方法正确输出与iostreams的Unicode。 有什么build议么? 这不起作用: SetConsoleOutputCP(CP_UTF8); utf8_locale = locale(old_locale,new boost::program_options::detail::utf8_codecvt_facet()); wcout.imbue(utf8_locale); wcout << L"¡Hola!" << endl; 编辑我找不到任何其他的解决scheme,而不是把这个片段包在一个stream中。 希望有人有更好的想法。 //Unicode output for […]

如何使用cout以完全精度打印双精度值?

所以我已经得到了最后一个问题的答案(我不知道为什么我没有想到这一点)。 我正在用cout打印一个double ,当我不期待的时候。 我怎样才能让cout使用全精度打印double精度?

为什么从std :: istream读取logging结构字段失败,我该如何解决?

假设我们有以下情况: 一个logging结构声明如下 struct Person { unsigned int id; std::string name; uint8_t age; // … }; logging使用以下格式存储在文件中: ID Forename Lastname Age —————————— 1267867 John Smith 32 67545 Jane Doe 36 8677453 Gwyneth Miller 56 75543 J. Ross Unusual 23 … 应读入文件以收集任意数量的上述Personlogging: std::istream& ifs = std::ifstream("SampleInput.txt"); std::vector<Person> persons; Person actRecord; while(ifs >> actRecord.id >> actRecord.name >> actRecord.age) […]

C ++:“std :: endl”vs“\ n”

许多C ++书籍包含这样的示例代码… std::cout << "Test line" << std::endl; …所以我也一直这样做。 但是我已经看到了很多来自这样的开发人员的代码: std::cout << "Test line\n"; 是否有技术上的理由相对于另一个,或只是一个编码风格的问题?

为什么std :: getline()在格式化提取之后跳过input?

我有以下代码提示用户input他们的名字和状态: #include <iostream> #include <string> int main() { std::string name; std::string state; if (std::cin >> name && std::getline(std::cin, state)) { std::cout << "Your name is " << name << " and you live in " << state; } } 我发现这个名字已经被成功提取出来了,但是却没有成功。 这里是input和结果输出: Input: "John" "New Hampshire" Output: "Your name is John and you live in " […]

为什么iostream :: eof里面的循环条件被认为是错误的?

我刚刚在这个答案中发现了一个评论,说在循环条件下使用iostream::eof是“几乎肯定是错误的”。 我通常使用类似while(cin>>n) – 我猜隐式检查EOF,为什么显式检查使用iostream::eof错误的iostream::eof ? 这与在C中使用scanf("…",…)!=EOF (我经常使用没有问题)有什么不同?

如何正确地重载ostream的<<运算符?

我正在用C ++写一个矩阵运算的小型矩阵库。 然而,我的编译器抱怨,之前没有。 这个代码被放置在一个架子上6个月,之间我升级我的电脑从debian蚀刻到lenny(g ++(Debian 4.3.2-1.1)4.3.2),但是我有相同的问题,在Ubuntu系统相同的g ++ 。 这是我的矩阵类的相关部分: namespace Math { class Matrix { public: […] friend std::ostream& operator<< (std::ostream& stream, const Matrix& matrix); } } 而“实施”: using namespace Math; std::ostream& Matrix::operator <<(std::ostream& stream, const Matrix& matrix) { […] } 这是编译器给出的错误: matrix.cpp:459:error:'std :: ostream&Math :: Matrix :: operator <<(std :: ostream&,const Math :: Matrix&)'必须只有一个参数 […]