gpt4 book ai didi

c++ - GCC 5.2.1 上的 EOF 与 basic_ios::eof()

转载 作者:行者123 更新时间:2023-11-30 02:27:04 25 4
gpt4 key购买 nike

在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com