0

我尝试将文本添加到现有文件的指定位置。当我运行我的代码时,在结束文件中添加了新文本,而不是在指定位置。我做错了什么?

fs=openFile "c:/my.txt" mode:"a+"
skipToString  fs ";"
seek fs (filepos -1)
format "add new text" to:fs
close fs

му.txt 内容

levl_name        # tech_lvl,military_lvl,desert_lvl;
body        # Column,Pilaster,Plinth,Wall_,Floor_;
prefix1        # _left_,_right_,_top_,_bottom_;
prefix2        # _bad_, _good_,_Tall_, _Low_;
4

2 回答 2

1

The alternative is to convert to a stringStream, do all your work in there and simply overwrite the file.

于 2014-10-31T00:33:39.780 回答
0

好吧,正如参考中明确指出的那样:'当使用“a”访问类型之一打开文件时,所有写操作都发生在文件末尾。文件指针可以使用 seek 重新定位,但总是在执行任何写操作之前移回文件末尾。

此外,写入流会覆盖内容,您必须获取从该位置到文件末尾的所有内容,保存它,进行编辑并再次粘贴,例如

file = @"c:\my.txt"

ms = MemStreamMgr.openFile file
size = ms.size()
MemStreamMgr.close ms

fs = openFile file mode:"r+"
skipToString fs ";"
pos = filePos fs - 1
seek fs pos
previousContent = readChars fs (size - pos)
seek fs pos
format "add new text" to:fs
format previousContent to:fs
close fs

如果您可以更改文件格式,请考虑使用 INI/XML 文件,使用它们会容易得多。

于 2014-10-21T11:53:20.163 回答