通过 Vim 保存平面文件,在文件中添加一个不可见的字节来创建新行

Laz*_*uli 1 linux vim diff flat-file character-encoding

标题并不具体,但我很难识别正确的关键词,因为我不确定这里发生了什么。出于同样的原因,我的问题可能有重复,如 。如果是这样的话:对不起!

我有一个通过平面文件接收数据的 Linux 应用程序。我不知道这些文件是如何生成的,但我可以毫无问题地读取它们。这些都是短文件,每个文件只有一行。

出于测试目的,我尝试修改其中一个文件并将其重新注入到应用程序中。但是当我这样做时,我可以在日志中看到它在消息末尾添加了一个神秘的分页符(导致应用程序无法识别该消息)...

举例来说,假设我收到一个名为original的平面文件,其中包含以下内容:

ABCDEF
Run Code Online (Sandbox Code Playgroud)

我复制了该文件并将其命名为copy

  • 如果我使用“diff”命令比较这两个文件,它会说它们是相同的(正如我所期望的那样)
  • 如果我通过 Vi 打开副本,然后退出而不更改也不保存任何内容,然后使用“diff”命令,它会说它们是相同的(正如我也期望它们一样)
  • If I open copy via Vi and then save it without changing anything and then use the "diff" command, I have the following (I added the dot for layout purpose):

diff original copy 1c1 < ABCDEF \ No newline at end of file --- .> ABCDEF

And if I compare the size of my two files, I can see that original is 71 bytes when copy is 72.

It seems that the format of the file change when I save the file. I first thought of an encoding problem, so I used the ":set list" command on Vim to see the invisible characters. But for both files, I can see the following:

ABCDEF$
Run Code Online (Sandbox Code Playgroud)

I have found other ways to do my test, But this problem still bugged me and I would really like to understand it. So, my two questions are:

  1. What is happening here?
  2. How can I modify a file like that without creating this mysterious page break?

Thank you for your help!

rom*_*inl 5

发生的情况是,Vim 默认设置为假设您编辑的文件以“换行”字符结尾。这是UNIX 领域的正常行为。但是你的程序正在读取的“文件”对我来说看起来更像是“流”,因为它们不以换行符结尾。

为了确保写入这些“文件”时没有换行符,请在写入之前设置以下选项:

:set binary noeol
Run Code Online (Sandbox Code Playgroud)

:help 'eol'