vote up 0 vote down star

I create a new blank .txt file and file size is 0 bytes.

Then I just add one character to the file and file size is 1 byte.

1000 byte file size could mean that it has 1000 characters?

flag

20% accept rate

migrated from stackoverflow.com

5 Answers

vote up 2 vote down

You have to know the difference between file size and disk usage. A file can have little content but use a lot more disk space.

link|flag
vote up 2 vote down

Depends on the encoding used. If you use ANSI enconding, then yes. It is one byte per char used. If you use UTF-8, for instance , you use more thant 1 byte (more like 3 bytes per character).

link|flag
2  
UTF-8 for ASCII also uses just one byte per code point. – Johannes Rössel Dec 3 at 16:11
vote up 0 vote down

I'm not sure what the actual question is here. Do you mean that it's saying it's 1KB (not 1 byte, but 1 kilobyte)? It's probably a case of windows rounding up to the nearest kilobyte. The file is almost certainly only using a byte of disk space, but XP measures file sizes in KB so it rounds up.

It doesn't round to 0 even though it's far closer to 0 than 1 because 0 implies the file is empty.

link|flag
vote up 0 vote down

When a file allocates disk space it does so in chunks based on the configuration used when the disk was formatted. Regardless of the amount of actually data (file size) the chunks need to be allocated to accommodate all of the data. For example using your example: An empty file (zero bytes) requires no chunks to be allocated. As soon as you add a single byte the OS needs to allocate a chunk to accommodate that data. On my XP system that seems to be 4KB. Therefore a file size of 1 byte will take up 4KB of space on the disk. If that chunk is filled to 1 byte over 4KB then another chunk will be allocated to make the size on disk equal to 8KB.

link|flag
vote up 0 vote down

Operating systems (windows, linux etc.) try to divide hard disk volume into pieces (technically called “clusters”), every one of which usually can contain many bytes of data, say 1024 bytes per cluster. Therefore, even if your file contains only 1 byte, the file will still occupy one cluster. If a file contains 1025 bytes, it will occupy two clusters. … and that’s why.

link|flag

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.