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
.
![ElektroStudios](https://i.stack.imgur.com/g9kGm.png?s=256&g=1)
Author by
ElektroStudios
Updated on June 20, 2022Comments
-
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 about 12 yearsoh tahnkyou, all the lines contained a "\n" chars solved: File.read(line[0..-2])
-
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.