In this case, why 1 byte extra when computing the file size? [closed]

十年热恋 提交于 2019-12-23 07:15:07

问题


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?


回答1:


It's due to text editor you are getting this. YOu must have pressed extra terminating character such as newline.

If you want 1 byte size for 1 character in a file.

Just do like this on you terminal.

#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

You will get exactly 1 byte file size.

Try it. (It's working on my system Ubuntu 11.04)




回答2:


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'.



来源:https://stackoverflow.com/questions/13717523/in-this-case-why-1-byte-extra-when-computing-the-file-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!