在使用GCC 5.2.1编译的程序中,当ifstream到达文件末尾时,它的状态不会切换到basic_ios::eof() --即在文件结束后条件if(eof())计算为false --而在Visual 2015上编译的相同代码的行为与预期相同:当达到文件结束时,basic_ios::eof()在if条件中计算为true。
我用if(bad())代替了if(bad()),然后用if(fail())替换了它们,所有这些都被评估为false。但是,当我放置EOF宏时,if(EOF)的计算结果是true-just,就像VS编译的程序上的if(eof())一样。
std::basic_ios::eof()为什么不使用GCC编译的程序呢?
下面是程序的代码
#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 7I 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 REST 5 6 11 Alle klasser 184 202 386的文本
发布于 2017-02-09 11:47:13
EOF是一个常量整数的宏,所以毫不奇怪
if (EOF && curr_boy == boys && curr_girl == girls)正在“工作”。它只是没有做您期望的事情,这是检查文件的结束。
只有在io操作失败时才应使用eof()。文档规定:
std::basic_ios::eof只报告由最新I/O operation.it设置的流状态,而不检查关联的数据源。例如,如果最近的I/O是get(),它返回文件的最后一个字节,则eof()返回false。next get()无法读取任何内容并设置eofbit。只有这样,eof()才返回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; }发布于 2017-02-09 11:47:28
您在同一个操作系统中使用相同的文件吗?
getline函数将设置eof标志,如果它在看到'\n'之前到达文件的末尾。这只能发生在文件的最后一行。
您可能应该将结束测试移到while-循环之外,在那里我们知道当最新的getline无法读取另一行时,我们已经到达了文件的末尾。
https://stackoverflow.com/questions/42134814
复制相似问题