在这个脚本中,f.seek(0)
的目的是什么?如果该文件已经被程序打开,为什么我们需要rewind(current_file)
?
input_file = ARGV[0]
def print_all(f)
puts f.read()
end
def rewind(f)
f.seek(0)
end
def print_a_line(line_count,f)
puts "#{line_count} #{f.readline()}"
end
current_file = File.open(input_file)
puts "First Let's print the whole file:"
puts # a blank line
print_all(current_file)
puts "Now Let's rewind, kind of like a tape"
rewind(current_file)
puts "Let's print the first line:"
current_line = 1
print_a_line(current_line, current_file)
rewind
的方法内部,这不会给你提示吗?如果不行,您能看一下文档吗? - user229044ri seek
。 - the Tin Man