2
MYFILE.TXT包含如何在文本文件中插入一行?
This is a sample file
This contains some data
End of file
我將創建內容的字符串(動態地)並且寫入文件內的陣列
我試圖與R +模式,但它會覆蓋現有的內容
file = File.open('myFile.txt','r+')
myString = 'Dynamic content' # varies accordingly for each file based on file
while (line = @file.gets)
next unless line =~ /sample file/
@file.puts(myString)
break
end
但它導致myFile.txt包含
This is a sample file
Dynamic content
e data
End of file
我有一個+模式嘗試使用和追加模式增加了最後的文件追加到最後
我試圖讓MYFILE.TXT作爲
This is a sample file
Dynamic content
This contains some data
End of file
是否有任何其他方式/在紅寶石模式做到這一點?
如果內存服務,在遙遠的過去文件中的一段時間是紙張(磁帶或卡)。 –
@CarySwoveland:哦是的,我見過工廠機器從[像這樣的代碼]工作(https://upload.wikimedia.org/wikipedia/commons/8/8d/%D0%9F%D0%B5%D1% 80%D1%84%D0%BE%D0%BA%D0%B0%D1%80%D1%82%D0%B0.jpg)。 –