c++ - C++/VIM 与其他 IDE 相比奇怪的 EOF 行为

标签 c++ vim eof

我目前在程序中正确设置 EOF 位时遇到问题。 该程序的要求规定我将有两个文件,它们都具有已排序的整数列表。该程序应该从两个文件中获取一个数字,然后比较这些数字。如果其中一个小于另一个,较小的一个将被输出到另一个文件中,然后另一个数字将从其原始数字所在的文件中抽取。

例如,假设文件 1 包含整数 1 3 5 9,整数之间有一个新行而不是空格,文件 2 包含 2 4 6 10。 1 应该与 2 进行比较,然后1 应绘制到输出文件中。然后 1 将被数字 3 替换。

为了帮助我解决这个问题,我研究了在绘制最后一个数字后再次尝试绘制数字后设置 EOF 标志。这在我下面通过 VIM 创建的输入文件的代码中完美运行。将文件 1 中的最后一个数字写入输出文件后,设置 EOF 位,然后程序引入第二个文件中的所有数字。

但是,如果我在任何 IDE 或文本编辑器中使用相同数量的字符之间的新空格编写相同的输入文件,我会看到一些奇怪的 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 文件来看出(不要这样做) .

关于c++ - C++/VIM 与其他 IDE 相比奇怪的 EOF 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29982324/

相关文章:

c++ - 如何在音轨中找到无声部分

c++ - 使用指针表示法访问结构数组

vim - 如何在 Vim 中以读/写模式打开文件?

c - 使用 fgets 到达 EOF

C++ 原生类型 char 可以保存文件结尾字符吗?

java - 通过网络发送文件时如何指定文件结尾

Boost.Range 的 C++ 元组 - 获取元素类型的元组?

c++ - 如何序列化并通过网络发送 std::list?

bash - 将 shell 的工作目录设置为在 vim 中打开的当前文件

vim - Nerd 树 : enter does not open sub dirs