【问题标题】:opening a .txt file created in vim in sublime在 sublime 中打开在 vim 中创建的 .txt 文件
【发布时间】:2016-06-28 17:14:02
【问题描述】:

所以我刚刚注意到在 vim 中创建的 sublime 中打开 .txt 文件时有些奇怪。

似乎 sublime 在 .txt 文件的末尾添加了一个空行。例如,如果我使用 vim/gedit 键入以下 2 行文件:

1
2

当我在 Sublime 中打开这个 .txt 文件时,它打开为

1
2
~

我用“~”来表示一个空行。有人可以试试这个并告诉我这对你来说是否一样?

这是我在 sublime 中创建 .txt 文件时使用的代码:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
  int      a;
  int      b;
  ifstream inFile;
  bool     validInputFound;

  inFile.open("inputVals.txt");

  if (inFile.fail())
  {
    cout << "Unable to open input file!" << endl;
    exit(1);
  }

  validInputFound = false;
  while (!validInputFound)
  {
    inFile >> a;

    if (inFile.eof())
    {
      cout << "EOF before reading a" << endl;
      exit(2);
    }
    else if (inFile.fail())
    {
      inFile.clear();
      inFile.ignore(200, '\n');
    }
    else
    {
      validInputFound = true;
    }
  }
  cout << "Read a: " << a << endl;
  validInputFound = false;

  while (!validInputFound)
  {
    inFile >> b;

    if (inFile.eof())
    {
      cout << "EOF before reading b" << endl;
      exit(2);
    }
    else if (inFile.fail())
    {
      inFile.clear();
      inFile.ignore(200, '\n');
    }
    else
    {
      validInputFound = true;
    }
  }
  cout << "Read b: " << b << endl;

  cout << "Sum: " << a + b << endl;
  inFile.close();

  return (0);
}

预期的输出是:

Read a: 1
Read b: 2
Sum: 3

但是如果你在 Sublime 中创建 inputVals.txt 文件,你会得到:

Read a: 1
EOF before reading b

【问题讨论】:

  • 你的意思是什么?您是否怀疑其中一位编辑添加了一些 CR/LF ?如果您有疑问,只需在十六进制编辑器中打开文件并检查。
  • 好吧,我的 OP 是在问一个问题,而不是在陈述一个观点。
  • 当然,但是这个问题对我来说有点离题,这就是我要求澄清的原因。如果您怀疑编辑在做奇怪的事情,而不是确保它是真实的,然后问“为什么 XXX 做 YYY”。对我来说,这只是一些显示行为,因此不适合 SO。此外,还有什么意义?从程序员的角度来看,XXX 在文件末尾显示一个空行有什么区别?
  • 哦,所以我目前正在学习 C++ 课程,当我在 sublime 中创建 input.txt 文件(代码读取)时,讲师提供的代码不起作用,但在 vim/ 中有效编辑。我会将代码编辑到 OP 中。
  • Sublime Text 有一个设置ensure_newline_at_eof_on_save。您可能已将设置设置为 true。

标签: sublimetext2


【解决方案1】:

看起来 vim 添加了换行符而不是 Sublime Text。

至于为什么:Why would Vim add a new line at the end of a file?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    相关资源
    最近更新 更多