ruby: `read': Invalid argument -(Errno::EINVAL) at File.read

ElektroStudios picture ElektroStudios · Apr 14, 2012 · Viewed 18.6k times · Source

I'm doing a simple script to check crc of all files...

require "zlib"
exit if Object.const_defined?(:Ocra)

files = Dir.glob("*")

File.open('dir.txt', 'a+') do |file|  
 file.puts files
end

File.read('dir.txt').each_line { |line|
    file = File.read(line) ; nil
    file_crc = Zlib.crc32(file,0).to_s(16)
    puts line, file_crc
}

The problem is at the line File.read('dir.txt').each_line { |line| I get this error:

test.rb:13:in `read': Invalid argument - 1.exe (Errno::EINVAL)
        from C:/Users/Administrador/Desktop/1.rb:13:in `block in <main>'
        from C:/Users/Administrador/Desktop/1.rb:12:in `each_line'
        from C:/Users/Administrador/Desktop/1.rb:12:in `<main>'

PD: 1.exe is a file listed in the "dir.txt".

Answer

Karoly Horvath picture Karoly Horvath · Apr 14, 2012

Have you checked that the line doesn't contain extra characters? p line.

IIRC line will contain the newline character, use line.chomp.