Search code examples
rubyfile-read

Ruby Reads Different File Sizes for Line Reads


I need to do something where the file sizes are crucial. This is producing strange results

filename = "testThis.txt"
total_chars = 0
file = File.new(filename, "r")
file_for_writing = nil
while (line = file.gets)
  total_chars += line.length
end
puts "original size #{File.size(filename)}"
puts "Totals #{total_chars}"

like this

original size 20121
Totals 20061

Why is the second one coming up short?

Edit: Answerers' hunches are right: the test file has 60 lines in it. If I change this line

  total_chars += line.length + 1

it works perfectly. But on *nix this change would be wrong?

Edit: Follow up is now here. Thanks!


Solution

  • There are special characters stored in the file that delineate the lines:

    • CR LF (0x0D 0x0A) (\r\n) on Windows/DOS and
    • 0x0A (\n) on UNIX systems.

    Ruby's gets uses the UNIX method. So, if you read a Windows file you would lose 1 byte for every line you read as the \r\n bytes are converted to \n.

    Also String.length is not a good measure of the size of the string (in bytes). If the String is not ASCII, one character may be represented by more than one byte (Unicode). That is, it returns the number of characters in the String, not the number of bytes.

    To get the size of a file, use File.size(file_name).