奇怪的 EOF 行为 C++/VIM 与其他 IDE
Weird EOF behavior C++/VIM versus other IDEs
我目前在使用我的程序正确设置 EOF 位时遇到问题。
这个程序的要求是我将有两个文件,它们都有排序的整数列表。该程序应该从两个文件中获取一个数字,然后比较这些数字。如果一个小于另一个,较小的一个将被输出到另一个文件中,然后另一个数字将从它获得原始数字的文件中提取。
因此,例如,假设文件 1 包含整数 1 3 5 9,整数之间换行而不是白色 space,文件 2 包含 2 4 6 10。1 应该与 2 进行比较,然后将 1 绘制到输出文件中。然后 1 将被数字 3 替换。
为了帮助我解决这个问题,我研究了在最后一个数字被绘制后尝试再次绘制数字后 EOF 标志被设置。这在我下面的代码中非常有效,输入文件是通过VIM。在文件 1 中的最后一个数字被提取到输出文件后,EOF 位被设置,然后程序从第二个文件中提取所有数字。
但是,如果我在任何 IDE 或文本编辑器中使用相同数量的字符之间的新 space 编写相同的输入文件,我会看到一些奇怪的 EOF 行为。当我在 Sublime Text2 或 Text Edit 中编写相同的文件时,EOF 位在绘制最后一个数字时设置正确。假设我们使用命令 inputFile2 >> number2,当此命令绘制最后的数字 10 时,EOF 将设置为 1。在通过 VIM 创建的文本文件中,如果我们使用命令 inputFile2 >> number2最后一个数字被绘制为 10,EOF 将不会设置为 1,直到我 运行 命令 inputFile2 >> number2.
有谁知道为什么两位编辑之间会有差异?我已经提供了下面的代码。有谁知道可能出了什么问题?感谢您的帮助。
std::ifstream inputFile;
std::ifstream inputFile2;
std::ofstream outputFile;
bool conditionMet = false;
/* string variables for user input for files */
std::string inputName;
std::string inputName2;
std::string outputName;
/* int variables to hold the input from file */
int number1,
number2;
inputFile.open("num1.txt");
inputFile2.open("num2.txt");
outputFile.open("output.txt);
inputFile >> number1;
inputFile2 >> number2;
/* loop until all of the numbers from files are in */
while (conditionMet == false)
{
/* if the first number is less than or equal to the second number */
/* check if the input file for number one is at the end of file */
if (number1 <= number2)
{
/* if the end of file has been reached for the input file 1 */
if (inputFile.eof())
{
/* put all of the numbers from input file 2 until the end of file for the second file */
while (!inputFile2.eof())
{
outputFile << number2 << '\n';
inputFile2 >> number2;
}
/* end the loop */
conditionMet = true;
}
/* if the end of the file has not been reached then output number from input file 1 and then take in a new number */
else if (!inputFile.eof())
{
std::cout << inputFile.eof() << "file1 eof before" << number1 << std::endl;
outputFile << number1 << '\n';
inputFile >> number1;
std::cout << inputFile.eof() << "file1 eof after" << number1 << std::endl;
}
}
/* if the first number is greater than the second number */
else if (number1 > number2)
{
/* check if the input file for number two is at the end of file */
/* if the end of file has been reached for the input file 2 */
if (inputFile2.eof())
{
/* put all of the numbers from input file until the end of file for the first file */
while (!inputFile.eof())
{
outputFile << number1 << '\n';
inputFile >> number1;
}
/* end the loop */
conditionMet = true;
}
/* if the end of the file has not been reached, then output number from input file 2 and then take in a new number */
else if (!inputFile2.eof())
{
std::cout << inputFile2.eof() << "file2 eof before" << number2 << std::endl;
outputFile << number2 << '\n';
inputFile2 >> number2;
std::cout << inputFile2.eof() << "file2 eof after" << number2 << std::endl;
}
}
}
/* close all files */
inputFile.close();
inputFile2.close();
outputFile.close();
return 0;
这是因为 VIM
(我的配置中 emacs
也是如此)总是以换行符结束文件。这样做是因为 VIM 是面向行的,对于 C/C++ 源代码,这确实是标准规定的。
事实上 VIM
确实非常适合由相对较短的行组成的可能很长的文件,这可以通过尝试处理由一行组成的 10Mb 文件来看出(不要那样做)。
我目前在使用我的程序正确设置 EOF 位时遇到问题。 这个程序的要求是我将有两个文件,它们都有排序的整数列表。该程序应该从两个文件中获取一个数字,然后比较这些数字。如果一个小于另一个,较小的一个将被输出到另一个文件中,然后另一个数字将从它获得原始数字的文件中提取。
因此,例如,假设文件 1 包含整数 1 3 5 9,整数之间换行而不是白色 space,文件 2 包含 2 4 6 10。1 应该与 2 进行比较,然后将 1 绘制到输出文件中。然后 1 将被数字 3 替换。
为了帮助我解决这个问题,我研究了在最后一个数字被绘制后尝试再次绘制数字后 EOF 标志被设置。这在我下面的代码中非常有效,输入文件是通过VIM。在文件 1 中的最后一个数字被提取到输出文件后,EOF 位被设置,然后程序从第二个文件中提取所有数字。
但是,如果我在任何 IDE 或文本编辑器中使用相同数量的字符之间的新 space 编写相同的输入文件,我会看到一些奇怪的 EOF 行为。当我在 Sublime Text2 或 Text Edit 中编写相同的文件时,EOF 位在绘制最后一个数字时设置正确。假设我们使用命令 inputFile2 >> number2,当此命令绘制最后的数字 10 时,EOF 将设置为 1。在通过 VIM 创建的文本文件中,如果我们使用命令 inputFile2 >> number2最后一个数字被绘制为 10,EOF 将不会设置为 1,直到我 运行 命令 inputFile2 >> number2.
有谁知道为什么两位编辑之间会有差异?我已经提供了下面的代码。有谁知道可能出了什么问题?感谢您的帮助。
std::ifstream inputFile;
std::ifstream inputFile2;
std::ofstream outputFile;
bool conditionMet = false;
/* string variables for user input for files */
std::string inputName;
std::string inputName2;
std::string outputName;
/* int variables to hold the input from file */
int number1,
number2;
inputFile.open("num1.txt");
inputFile2.open("num2.txt");
outputFile.open("output.txt);
inputFile >> number1;
inputFile2 >> number2;
/* loop until all of the numbers from files are in */
while (conditionMet == false)
{
/* if the first number is less than or equal to the second number */
/* check if the input file for number one is at the end of file */
if (number1 <= number2)
{
/* if the end of file has been reached for the input file 1 */
if (inputFile.eof())
{
/* put all of the numbers from input file 2 until the end of file for the second file */
while (!inputFile2.eof())
{
outputFile << number2 << '\n';
inputFile2 >> number2;
}
/* end the loop */
conditionMet = true;
}
/* if the end of the file has not been reached then output number from input file 1 and then take in a new number */
else if (!inputFile.eof())
{
std::cout << inputFile.eof() << "file1 eof before" << number1 << std::endl;
outputFile << number1 << '\n';
inputFile >> number1;
std::cout << inputFile.eof() << "file1 eof after" << number1 << std::endl;
}
}
/* if the first number is greater than the second number */
else if (number1 > number2)
{
/* check if the input file for number two is at the end of file */
/* if the end of file has been reached for the input file 2 */
if (inputFile2.eof())
{
/* put all of the numbers from input file until the end of file for the first file */
while (!inputFile.eof())
{
outputFile << number1 << '\n';
inputFile >> number1;
}
/* end the loop */
conditionMet = true;
}
/* if the end of the file has not been reached, then output number from input file 2 and then take in a new number */
else if (!inputFile2.eof())
{
std::cout << inputFile2.eof() << "file2 eof before" << number2 << std::endl;
outputFile << number2 << '\n';
inputFile2 >> number2;
std::cout << inputFile2.eof() << "file2 eof after" << number2 << std::endl;
}
}
}
/* close all files */
inputFile.close();
inputFile2.close();
outputFile.close();
return 0;
这是因为 VIM
(我的配置中 emacs
也是如此)总是以换行符结束文件。这样做是因为 VIM 是面向行的,对于 C/C++ 源代码,这确实是标准规定的。
事实上 VIM
确实非常适合由相对较短的行组成的可能很长的文件,这可以通过尝试处理由一行组成的 10Mb 文件来看出(不要那样做)。