我使用的是 Delphi 7.0,需要能够写入文本文件的中间。这是我的程序创建的文本文件的示例。
~V
VERS. 2.0: CWLS LOG ASCII STANDARD - VERSION 2.0
WRAP. NO : One line per depth step
~W
STRT.Ft 10000 : Start Depth
STOP.Ft 11995 : Stop Depth
STEP.Ft 5 : Step
... A bunch of data follows.
现在,当我最初将值写入文本文件时,我想记住上例中 STOP 值 11995 的文件位置。现在,一段时间后我的数据将发生变化,我想移动到 11995 的位置并写入新的止损值。这样我就不需要重写文件中的所有内容。
最佳答案
使用标准 Pascal 文件 I/O,您只能读取、重写或追加文件中的数据。
如果你想改变文件某个位置的数据,你可以使用TFileStream:
var
f:TFileStream;
PositionStr:String;
PositionValue:Integer;
begin
f := TFileStream.Create('filename.log',fmOpenReadWrite);
PositionValue := 200000; // new STOP Position
PositionStr := IntToStr(PositionValue);
f.Seek(100,soFromBeginning); // Data will be overwritten from position 100
f.WriteBuffer(PositionStr[1], length(PositionStr));
f.free;
end;
关于delphi - 写入文本文件的中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17011971/