Kurt Peek
Kurt Peek

Reputation: 57421

Why is a text file containing only the word "hello" 6 (and not 5) bytes in size?

As I understand it, one character is one byte in size. As a test, I made (using Gedit) two text files, one called hello.txt containing only the word "hello", and similarly, one called goodbye.txt containing only the word "goodbye". This is how they look with ls -lhtr:

enter image description here

It would appear that hello.txt, which contains 5 characters, is 6 bytes in size, and goodbye.txt, which contains 7 characters, is 8 bytes in size. To generalize, it seems like a file with n characters is n+1 bytes large. Can someone explain to me where the extra byte comes from?

Upvotes: 0

Views: 1518

Answers (2)

Abdul RAFAY ZIA
Abdul RAFAY ZIA

Reputation: 1

yes, in character strings on byte is use for null (/0) for termnating the string so its byte is always greater than original array length..

Upvotes: -2

clemep
clemep

Reputation: 124

Your editor is most likely inserting a newline character into the file. On Linux, that would be a one byte '\n' (newline) character. On some OSes, that would be the two byte sequence '\r\n' (carriage return, newline).

Check the file contents with: od -c <filename>

That will show the byte-by-byte contents.

Upvotes: 3

Related Questions