Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby中的.each循环中读取文件时移动到文件的最后一行_Ruby_File - Fatal编程技术网

在Ruby中的.each循环中读取文件时移动到文件的最后一行

在Ruby中的.each循环中读取文件时移动到文件的最后一行,ruby,file,Ruby,File,我正在读取一个可以包含任意行数的文件 我只需要保存前1000个左右,作为变量“recordsToParse”传入 如果我达到我的1000行限制,或无论它设置为什么,我需要将拖车信息保存在文件中,以验证总计记录,总计金额等 因此,我需要一种方法将我的“指针”从文件中的任何位置移动到最后一行,并再次运行 file = File.open(file_name) parsed_file_rows = Array.new successful_records, failed_records = 0, 0

我正在读取一个可以包含任意行数的文件

我只需要保存前1000个左右,作为变量“
recordsToParse
”传入

如果我达到我的1000行限制,或无论它设置为什么,我需要将拖车信息保存在文件中,以验证
总计记录
总计金额

因此,我需要一种方法将我的“指针”从文件中的任何位置移动到最后一行,并再次运行

file = File.open(file_name)

parsed_file_rows = Array.new
successful_records, failed_records = 0, 0
file_contract = file_contract['File_Contract']
output_file_name = file_name.gsub(/.TXT|.txt|.dat|.DAT/,'')

file.each do |line|
  line.chomp!
  line_contract = determine_row_type(file_contract, line)

  if line_contract
    parsed_row = parse_row_by_contract(line_contract, line)
    parsed_file_rows << parsed_row
    successful_records += 1
  else
    failed_records += 1
  end

  if (not recordsToParse.nil?)
    if successful_records > recordsToParse
      # move "pointer" to last line and go through loop once more
      #break;
    end
  end

end
store_parsed_file('Parsed_File',"#{output_file_name}_parsed", parsed_file_rows)
[successful_records, failed_records]
file=file.open(文件名)
已解析的\u文件\u行=Array.new
成功的\u记录,失败的\u记录=0,0
文件合同=文件合同[‘文件合同’]
output_file_name=file_name.gsub(/.TXT |.TXT |.dat |.dat/,“”)
文件。每个do |行|
line.chomp!
行\合同=确定行\类型(文件\合同,行)
如果行(u)合同
parsed_row=按合同解析_row_(行合同,行)
解析的\u文件\u行记录STOPARSE
#将“指针”移到最后一行并再次通过循环
#中断;
结束
结束
结束
存储解析的文件('解析的文件',“#{output_file_name}_parsed',解析的文件行)
[成功的\u记录,失败的\u记录]

使用
IO.seek
IO::seek\u END
将指针移动到文件的末尾,然后向上移动到最后一个CR,最后一行就到了

只有当文件非常大时,这才是值得的,否则只需按照
file.each do | line
到最后一行,或者您可以像这样读取最后一行
IO.readlines(“file.txt”)[-1]

最简单的解决方案是使用gem-like-elif

require "elif"

lastline = Elif.open("bigfile.txt") { |f| f.gets }

它使用seek快速读取您的最后一行。

这是我利用操作系统的
head
tail
命令的其中一次,使用类似以下命令:

head = `head -#{ records_to_parse } #{ file_to_read }`.split("\n")
tail = `tail -1 #{ file_to_read }

head.pop if (head[-1] == tail.chomp)
File.open(new_file_to_write, 'w') do |fo|
  fo.puts head, tail
end
然后用如下方式将其全部写出来:

head = `head -#{ records_to_parse } #{ file_to_read }`.split("\n")
tail = `tail -1 #{ file_to_read }

head.pop if (head[-1] == tail.chomp)
File.open(new_file_to_write, 'w') do |fo|
  fo.puts head, tail
end

IO.readlines(“file.txt”)[-1]
不是一个可扩展的解决方案。OP不知道文件有多大,
IO.readlines(“file.txt”)
将在尝试访问最后一行之前将每一行读取到内存中。海田先生,我仍在编辑,经过您的更正,我的其余文本丢失了,但无论如何,谢谢。关于你的评论,这就是为什么我说只有在文件不大的情况下才可用,我建议使用elif解决方案,如果你没有额外的宝石,则使用IO.seek解决方案。是的,整个测试的原因是文件太大,无法读入内存,因此我们正在传递一个参数1000@Geoff,您最后使用了什么解决方案?你能接受其中一个答案吗?或者请提供你自己的答案?