Why do I get one byte more reported? I do not have a newline at the end. I tested other files with similar result. Using linux 3.2.

$ cat testfile.txt 

$ wc -c testfile.txt 
7 testfile.txt

Similarly I get one more number of bytes reported if I use c++:

file(filename, std::ifstream::in | std::ios::binary);
file.seekg(0, std::ifstream::end);
int fsize = file.tellg();

The text aabbcd consist of 6 characters, in ASCII encoding that should be 6 bytes. Why do I get 7 bytes reported?

Note: I happend to have characters in the example, but I care only about the bytes, not text, not formatted input.

migrated from stackoverflow.com Aug 2 '13 at 5:55

This question came from our site for professional and enthusiast programmers.

  • 2
    Could you do od -x testfile.txt on the file and add the result to your question? – Joachim Isaksson Jul 26 '13 at 7:41
  • 1
    Maybe there's a stray line feed at the end of it? – user34080 Jul 26 '13 at 7:45

vi and many other editors add an extra line feed to the end of the file even if you don't add one manually. For example, writing aabbcd in vi, not pressing return and saving gives a file that od -x dumps as;

0000000      6161    6262    6463    000a

...which is (a little endian dump of) aabbcd + line feed.

ls -l will also show the file as 7 bytes;

$ ls -l testfile.txt 
-rw-r--r--  1 me  staff  7 Jul 26 09:52 testfile.txt

Most likely, this is the case with your file too.

  • I even get that with: $ echo -e '\n' > linefeed2; $ od -A n -x linefeed2; which prints: 0a0a – user1358 Jul 26 '13 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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