0
f= File.open('path_to_file','w')


f.lineno
#=> 0

f.gets
#=>"this is the content of the first line"

f.lineno

#=>1 # the lineno cooresponse to the next read point of IO#gets

f.rewind

f.lineno

#=>0

f.read

#=>"all the content in the file"

f.lineno

#=>0 # the lineno still is the beginning 

f.read 

#=>"" # but I can't get anyting , it seems like the read point reach to the end of the file, so the f.lineno should be 3, instead of 0

または、IOストリームの次の読み取りポイントを知る他の方法はありますか

f.lineno

#=>0
4

1 に答える 1

3

Ruby IO docsから、linenoストリーム内の位置はわかりません。むしろ、gets が呼び出された回数がわかります。関数は を使用しreadないため、値は変化しません。getslineno

おそらく必要なのはpos、ファイル内の現在のオフセットをバイト単位で示す です。posファイル内の別の場所にジャンプするように設定することもできます。

于 2012-06-30T05:46:35.703 に答える