我有一个让我发疯的问题。我写了这个非常简单的脚本来读取带有数字的两个文本文件。你可以忽略排序部分,这些数字应该是预先排序的,这应该是一个超级简单的脚本。
为了测试这个,我只尝试了两个文件,每个文件中只有一个数字。当我用g ++编译它时,没问题。但是使用VS 2013时,在读取最后一行而不是之后,为file1设置EOF标志。所以它永远不会写入文件中的最后一个数字。这不是file2的问题。它在别处编译时有效。任何人都可以帮助我吗?
file1.open("file1.txt");
if (file1.fail())
{
cout << "There has been an issue in opening file 1";
return 1;
}
file2.open("file2.txt");
if (file2.fail())
{
cout << "There has been an issue in opening file 2";
return 2;
}
file3.open("file3.txt", ios::out);
int num1;
int num2;
file1 >> num1;
file2 >> num2;
while (!file1.eof() || !file2.eof())
{
cout << "Current num1: " << num1 << endl;
cout << "Current num2: " << num2 << endl;
cout << "Current file1 state: " << file1.eof() << endl;
cout << "Current file2 state: " << file2.eof() << endl;
if (!file1.eof() && !file2.eof())
{
//cout << "Inequality state: " << (num1 < num2) << endl;
if (num1 < num2)
{
cout << num1 << endl;
file3 << num1 << endl;
if (!file1.eof())
file1 >> num1;
continue;
}
if (num2 < num1)
{
cout << num2 << endl;
file3 << num2 << endl;
if (!file2.eof())
file2 >> num2;
continue;
}
if (num1 == num2)
{
cout << num1 << endl;
cout << num2 << endl;
file3 << num1 << endl;
file3 << num2 << endl;
if (!file1.eof())
file1 >> num1;
if (!file2.eof())
file2 >> num2;
}
continue;
}
if (file1.eof())
{
cout << num2 << endl;
file3 << num2 << endl;
file2 >> num2;
continue;
}
if (file2.eof())
{
cout << num1 << endl;
file3 << num1 << endl;
file1 >> num1;
continue;
}
}