In this case, why 1 byte extra when computing the

2019-06-15 13:35发布

问题:

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