Writing to the middle of a file in ruby

I opened the file in ruby ​​with a + parameters. I can search the middle of the file and read from it, but when I try to write records, it always goes all the way. How to write a record in the middle?

jpg = File.new("/tmp/bot.jpg", "a+") jpg.seek 24 puts jpg.getc.chr jpg.seek 24 jpg.write "R" jpg.seek 28 jpg.write "W" puts jpg.pos jpg.close 

R and W end at the end of the file.

I know that I can only overwrite existing bytes, that’s fine, that’s what I want to do.

+4
source share
1 answer

This is what you request with the "a+" mode: make sure that all records always go to the end, allowing you to read and search (looking only for the meaning to read, of course, taking into account the mode). Use "r+" if you do not want all entries to always end.

+7
source

All Articles