- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在使用 GCC 5.2.1 编译的程序中,ifstream
的状态在到达文件末尾时不会切换到 basic_ios::eof()
— 即,条件 if(eof())
在到达文件末尾后评估为 false
— 而在 Visual Studio 2015 上编译的相同代码的行为符合预期:当到达文件末尾时,basic_ios::eof()
在 if
条件中的计算结果为 true
。
我用 if(bad())
替换了 if(eof())
,然后用 if(fail())
替换了,并且所有这些都评估为 false
。但是,当我改为放置 EOF
宏时,if(EOF)
的计算结果为 true
——就像 if(eof( ))
在VS编译的程序上做。
std::basic_ios::eof()
无法处理使用 GCC 编译的程序的原因可能是什么?
PS:下面是程序代码
#include <string>
#include <iostream>
#include <fstream>
#include <regex>
using namespace std;
using namespace std::chrono;
int ex10()
{
ifstream in{ "school.txt" };
if (!in) cout << "The 'school.txt' file was not opened.\n";
string line;
regex header{ R"(^([\w]+)(\t{2}\w+\s*\w*)(\t\w+\s*\w*)(\t\w+\s*\w*)$)" };
regex row{ R"(^(\w+\s*\w*)(\t{1,2}\d+)(\t{2}\d+)(\t{2}\d+)$)" };
if (getline(in, line)) {
smatch matches;
if (!regex_match(line, matches, header))
cerr << "Wrong header format.\n";
}
int linenum = 0;
int boys = 0;
int girls = 0;
ofstream out{ "schoolCompressed.txt" };
if (!out) cout << "The output file was not created.\n";
string prevLine;
int accumBoys;
int accumGirls;
int accumTot;
while (getline(in, line)) {
++linenum;
smatch matches;
if (!regex_match(line, matches, row))
cerr << "Row #" << linenum << " doesn't match the format.\n";
int curr_boy = stoi(matches[2]);
int curr_girl = stoi(matches[3]);
int curr_total = stoi(matches[4]);
if (curr_boy + curr_girl != curr_total)
cerr << "Wrong children number in line #" << linenum << '\n';
if (line[0] != prevLine[0]) {
if (linenum != 1) out << prevLine[0] << "\t\t" << accumBoys << "\t\t"
<< accumGirls << "\t\t" << accumTot << '\n';
accumBoys = curr_boy;
accumGirls = curr_girl;
accumTot = curr_total;
}
else if (line[0] == prevLine[0]) {
accumBoys += curr_boy;
accumGirls += curr_girl;
accumTot += curr_total;
}
if (EOF && curr_boy == boys && curr_girl == girls) { out << line; return 0; } //this works on GCC
//if (in.eof() && curr_boy == boys && curr_girl == girls) { out << line; return 0; } <= this works on VS 2015
boys += curr_boy;
girls += curr_girl;
prevLine = line;
}
cerr << "Somehow the program didn't manage to complete its task :(.\n";
return 1;
}
int main()
{
ex10();
}
school.txt
文件的文本
KLASSE DRENGE PIGER ELEVER 经典款
0A 12 11 23
1A 7 8 15
1B 4 11 15
2A 10 13 23
3A 10 12 22
4A 7 7 14
4B 10 5 15
5A 19 8 27
6A 10 9 19
6B 9 10 19
7A 7 19 26
7G 3 5 8
7我7 3 10
8A 10 16 26
9A 12 15 27
0MO 3 2 5
0P1 1 1 2
0P2 0 5 5
10B 4 4 8
10CE 0 1 1
1MO 8 5 13
2CE 8 5 13
3DCE 3 3 6
4MO 4 1 5
6CE 3 4 7
8CE 4 4 8
9CE 4 9 13
休息 5 6 11
Alle klasser 184 202 386
最佳答案
EOF 是一个常量整数的宏,所以
if (EOF && curr_boy == boys && curr_girl == girls)
正在“工作”。它只是没有按照您的预期进行,即检查文件末尾。
您应该仅在 io 操作失败时使用 eof()
。doc规定
std::basic_ios::eof only reports the stream state as set by the most recent I/O operation.it does not examine the associated data source. For example, if the most recent I/O was a get(), which returned the last byte of a file, eof() returns false. The next get() fails to read anything and sets the eofbit. Only then eof() returns true
在你的代码中你有
while (getline(in, line)) {
...
if (in.eof())
...
}
预期的行为是 GCC 的行为之一。要知道您位于文件末尾,您应该尝试进一步阅读。这是一个类似的问题
while (!in.eof()) {
...
getline(in, line)
...
}
您应该将 curr_boy 和条件移出 while 循环并消除 eof 测试
int curr_boy = 0;
int curr_girl = 0;
while (getline(in, line)) {
}
if (curr_boy == boys && curr_girl == girls) { out << line; return 0; }
关于c++ - GCC 5.2.1 上的 EOF 与 basic_ios::eof(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42134814/
以前有人遇到过这个错误吗?我不知道究竟是什么导致了这种情况: 1>...include\sstream(640): error : "std::basic_ios::basic_ios(const s
这是我的第一篇文章。 我看了很多题目,似乎我做的还不错,但是当我尝试编译下面的代码时,我又遇到了上面的错误: // Header file #include #include #include
我无法理解这个错误。这个错误不在我正在调试的类中。 (是吗?)错误是: c:\program files\microsoft visual studio 10.0\vc\include\fstream
收到此错误,我很确定它在 operator::basic_ios' : cannot access private member declared in class 'std::basic_ios'
while(true) { int a, c; string b; file >> a >> b >> c; if( file.good() ) f(a, b, c);` else b
我似乎无法发现这里的错误,其他文章对错误的答案有点模糊所以这里是我的。我收到此错误,我相信它与它试图打开的文件有关。我发布了整个 .cpp 文件,因为我不确定错误的来源。 #include #inc
我正在将一个大型 VC++ 6.0 应用程序转换为 VS2010,并且在其中一个项目中一直遇到此错误: error C2248: 'std::basic_ios::basic_ios' : canno
我发现自己对 rdstate() 标志反复感到困惑 - good()、bad()、eof() 、fail() - 以及它们在 basic_ios::operator!、operator bool 和
C++ IO 流的基类 std::basic_ios 定义了 operator void*() 以返回 !fail() 和 operator!() 返回 fail()。这让我想知道为什么我们根本需要
C++11 §27.5.4.2/21: void swap(basic_ios& rhs); Effects: The states of *this and rhs shall be exchang
我得到了以下错误,在花了数周时间(非常兼职)从我的代码中排除错误后,情况略有改善: 1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\
我正在使用 qteSTLib、C++(clang LLVM 8.0 版)编写文件读取单元测试。我有以下逐行读取文件的代码。 std::ifstream infile; try { infile
std::basic_ios has a public constructor : explicit basic_ios (std::basic_streambuf* sb); IMO,类拥有公共(p
在使用 GCC 5.2.1 编译的程序中,ifstream 的状态在到达文件末尾时不会切换到 basic_ios::eof() — 即,条件 if(eof()) 在到达文件末尾后评估为 false —
嘿,我收到一个错误,我认为这与从阅读其他帖子中复制 ofstream 变量有关,我已经尝试更改 std::ofstream outfil; 到 std::ofstream & outfil; 但是我得
嘿,我收到一个错误,我认为这与从阅读其他帖子中复制 ofstream 变量有关,我已经尝试更改 std::ofstream outfil; 到 std::ofstream & outfil; 但是我得
我正在将一些代码移植到 Darwin OS X,作为更改的一部分,我们从 gcc 转到 clang 编译器。 代码中有一个2005年的函数,在网上多处贴过。它为几个不同的旧版本 GCC 提供了功能,我
这个特定方法有问题,不知道如何解决!我得到的错误是上面的: "error C2248: 'std::basic_ios::basic_ios' : cannot access private memb
在创建一个简单的异常类扩展(我可以在其中更轻松地构建错误消息)时,我将错误隔离为以下简单代码: #include #include class myCout { public: std::
我的代码在到达 eof 并抛出异常时设置 std::failbit我怎样才能跳过eof异常 在 catch block 中,如果异常是因为 eof 但它不好,我会检查并跳过。 请建议我如何在下面的代码
我是一名优秀的程序员,十分优秀!