Cheap and Secure Web Hosting Provider : See Now

How a file is determined

, , No Comments
Problem Detail: 

If I have the integer ""1234" stored in a file, the size of the file when I use the command "ls -l" or "wc -c" is 5. If I have "12345" the size of the file is 6. Basically the number of bytes in a file = (number of individual digits) + 1. Now, I am assuming the "+1" comes from the space it takes to create the name of the file. But why is the rest of the byte count equal to the number of individual characters? Does the file system read the file character by character even we "write an integer" to the file? Elaboration on this would be very helpful.

Asked By : Jonathan
Answered By : D.W.

The +1 is for the newline character.

The byte count equals the number of characters, in this case, because each character takes exactly one byte. That need not be true in general (see, e.g., UTF-8), but it is for the example you listed.

You didn't write an integer to the file. You wrote a string -- a sequence of characters, namely the character 1, then the character 2, then 3, then 4, then a newline. Files don't store integers or other types. They store a sequence of bytes. Each character is converted to one or more bytes using an encoding, and then the sequence of bytes is written.

Best Answer from StackOverflow

Question Source : http://cs.stackexchange.com/questions/55364

3200 people like this

 Download Related Notes/Documents

0 comments:

Post a Comment

Let us know your responses and feedback