Chinaunix

标题: 一个关于回车键的比较弱智的问题 [打印本页]

作者: goingstudy    时间: 2013-01-16 09:50
标题: 一个关于回车键的比较弱智的问题
在Linux 下 文本文件中按回车键时是输入的ascii的\n吗,我试了一下,怎么好像不是:
下面是我操作的过程:
用vim新建b.dat,然后输入一个回车,保存退出
用hexdump -C b.dat 查看:

为什么有两个\n?
作者: goingstudy    时间: 2013-01-16 10:09
zi ji ding ....
作者: chishanmingshen    时间: 2013-01-16 11:13
没啥吧,肯定是kernel自动给加了一个回车呗.
作者: goingstudy    时间: 2013-01-16 11:15
那kernel为什么要再加一个呢?想不太明白

作者: T-Bagwell    时间: 2013-01-16 12:59
还有一个文件结束符
你不输入回车,写入一个字符的话,里面一样会有的
作者: tempname2    时间: 2013-01-16 13:27
VIM自己加的,可能unix下有“所有文件文件必需以换行结尾”的惯例。

# echo -n a > /tmp/abc
# hexdump /tmp/abc
0000000 0061                                   
0000001

#vi /tmp/abc

提示

"/tmp/c" [noeol] 1L, 1C

此时只是进入插入模式,什么也不做就退出,也会新加一个换行。
作者: tempname2    时间: 2013-01-16 13:29
:h noeol

                              'endofline' 'eol' 'noendofline' 'noeol'
'endofline' 'eol'       boolean (default on)
                              local to buffer
                             {not in Vi}
        When writing a file and this option is off and the 'binary' option
        is on, no <EOL> will be written for the last line in the file.  This
        option is automatically set when starting to edit a new file, unless
        the file does not have an <EOL> for the last line in the file, in
        which case it is reset.  Normally you don't have to set or reset this
        option.  When 'binary' is off the value is not used when writing the
        file.  When 'binary' is on it is used to remember the presence of a
        <EOL> for the last line in the file, so that when you write the file
        the situation from the original file can be kept.  But you can change
        it if you want to.

作者: goingstudy    时间: 2013-01-16 14:31
谢谢各位,明白了




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2