12

I made this following observations after making a text file on Ubuntu 12.10 through GUI (alt+enter) as well as in Terminal (ls -l).

  • when file was empty : file size = 0 byte.
  • when one character : file size = 2 bytes.
  • when two characters : file size = 3 bytes.

Why 1 byte extra when the file just contains one character, i know this is not because of End-Of-File because when i wrote a c program the loop terminated when it reached End-Of-File and it gave me the same results, so obviously this doesn't count, then what is it?

But on windows when the file contained one character the file size was just 1 byte, it was normal. What are the things behind all this stuffs?

4

2 回答 2

15

这是由于文本编辑器你得到这个。您必须按下额外的终止字符,例如换行符。

如果您想要1 byte文件中 1 个字符的大小。

就在你的终端上这样做。

#cat > file // create a file for input
x{CTRL+D}  // after inputting one character 'x' , press CTRL+D twice
           // one for terminating file and one for killing the cat process.

#ls -l  // list the file

您将获得精确的1字节文件大小。

试试看。(它在我的系统 Ubuntu 11.04 上运行)

于 2012-12-05T06:34:33.483 回答
7

Most likely the extra character is the newline character at the end of each line in a Unix/Linux text file. You should be able to see that char using the command 'od -c file'.

于 2012-12-05T06:31:09.050 回答