如何在某文本文档(共60行)里的第5行的下一行追加新的文本

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: 如何在某文本文档(共60行)里的第5行的下一行追加新的文本

Re: 如何在某文本文档(共60行)里的第5行的下一行追加新的文本

Post by tmplinshi » 02 May 2015, 02:56

另一个办法是用正则替换:

Code: Select all

AppendText(FileName, LineNum, Text) {
	f := FileOpen(FileName, "r")
	dat := f.Read()
	encoding := f.Encoding
	f.Close()

	dat := RegExReplace(dat, "^(.*?(\R)){" . LineNum . "}", "$0" . Text . "$2")
	FileOpen(FileName, "w", encoding).Write(dat)
}

Re: 如何在某文本文档(共60行)里的第5行的下一行追加新的文本

Post by tmplinshi » 01 May 2015, 12:51

先读取前 5 行,然后接上要追加的文本,以及剩余的内容。

Code: Select all

AppendText("某文本文档.txt", 5, "新的文本")

AppendText(FileName, LineNum, Text) {
	f := FileOpen(FileName, "r")

	content_ := ""
	Loop, % LineNum
		content_ .= f.ReadLine()
	newContent := content_ . Text . "`r`n" f.Read()

	encoding := f.Encoding
	f.Close()

	FileOpen(FileName, "w", encoding).Write(newContent)
}

如何在某文本文档(共60行)里的第5行的下一行追加新的文本

Post by autu » 01 May 2015, 04:01

如题 : 我要在某文本文档中间追加新的文本。




谢谢···········!

Top