0
$\begingroup$

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.

$\endgroup$

closed as off-topic by David Richerby, Tom van der Zanden, Raphael Apr 3 '16 at 8:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about computer science, within the scope defined in the help center." – David Richerby, Tom van der Zanden, Raphael
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ There'll be an end of line character at the end of the file. But this isn't a computer science question. $\endgroup$ – David Richerby Apr 3 '16 at 3:00
  • $\begingroup$ This is about how a certain OS and/or filestystem implement stuff, not about CS. $\endgroup$ – Raphael Apr 3 '16 at 8:23
  • $\begingroup$ But I feel as if it is relevant, since much of computer science has to do with data science, and knowing how a file system works is relevant knowledge. $\endgroup$ – Jonathan Apr 3 '16 at 18:40
2
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ So even when I use the command outfile.write(32); where write takes in an "int" as a param, it would still encode it as a char right? $\endgroup$ – Jonathan Apr 2 '16 at 23:55
  • 3
    $\begingroup$ @JonathanSmit, that's a programming question that will depend on what that language/library does, which is off-topic here. You'll need to consult the documentation or ask on Stack Overflow to figure out what that specific method does. $\endgroup$ – D.W. Apr 2 '16 at 23:59

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