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

19,360

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

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

Share:
19,360
ElektroStudios
Author by

ElektroStudios

Updated on June 20, 2022

Comments

  • ElektroStudios
    ElektroStudios about 2 years

    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".

  • ElektroStudios
    ElektroStudios about 12 years
    oh tahnkyou, all the lines contained a "\n" chars solved: File.read(line[0..-2])
  • Holger Just
    Holger Just about 12 years
    File.read(line.strip) is probably the more robust variant. Esp. if you have windows line-endings of \r\n. Although you then can't have spaces at the beginning and end of the filename, don't know if windows even allows that.