Ruby/File Directory/seek

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

position the file pointer 5 bytes from the end of the file and change the character to an X

IO::SEEK_CUR      Seeks a certain number of bytes ahead of the current position.
IO::SEEK_END      Seeks to a position based on the end of the file. 
IO::SEEK_SET      Seeks to an absolute position in the file. Identical to pos=.
 
f = File.new("test.txt", "r+")
f.seek(-5, IO::SEEK_END)
f.putc "X"
f.close



print every fifth character in a file:

f = File.new("test.txt", "r")
while a = f.getc
  puts a.chr
  f.seek(5, IO::SEEK_CUR)
end