Forum > Beginners

Write to TextFile on specific location

(1/2) > >>

chim:
Hello pros!

could you please help me?

I just want to write in a Textfile some Text, but this in the specific location, and i always get with the seek command an error back.

This is my short Code:

var
  F: TextFile;
begin
   AssignFile(F, 'C:\test\neu.html');
   Append(F);
   FileSeek(F,175);
   Writeln(F, 'Right Place');
   Writeln(F, 'dont overwrite');
   CloseFile(F);
end;

without the Seek command it works fine, but with the command i get this error back:

unit1.pas(102,10) Error: Call by var for arg no. 1 has to match exactly: Got "Text" expected "File"

So i understand what it say to me, but i couldn't figure out how to solve this, every other command don't have a problem with this,
could you please help me pro's?

thx in advance :)

marcov:
The error is correct. Textfiles are not seekable.

chim:
thx for ur reply,

can i have a chance to get this problem solved, with some other commands?

Bart:
Use a TStringList to load the test (LoadFromFile).
Find the location you need, insert the text and write back to file (SaveToFile)

Bart

howardpc:
You can force textfiles to be seekable with the following routine. I don't recommend it (because it hacks the normally unseen TTextRec file metadata), but it seems to work, at least on Windows. I have not tested on other platforms.


--- Code: ---function TextFileSeek(var F: TextFile; bytePos: integer): boolean;
const
  ErrorPos = $FFFFFFFF;
var
  FPointerPos: DWORD;
begin
  Result:=False;
  case TTextRec(F).Mode of
    fmInput:  begin
                FPointerPos:=SetFilePointer(TTextRec(F).Handle, bytePos, nil, FILE_BEGIN);
                if (FPointerPos = ErrorPos) then
                  SysErrorMessageUTF8(GetLastError)
                else Result:=True;
                TTextRec(F).bufend:=0;
              end;
    fmOutput: begin
                FPointerPos:=SetFilePointer(TTextRec(F).Handle, bytePos, nil, FILE_BEGIN);
                if (FPointerPos = ErrorPos) then
                  SysErrorMessageUTF8(GetLastError)
                else Result:=True;
                TTextRec(F).bufpos:=0;
              end;
    else
      SysErrorMessageUTF8(ERROR_FILE_NOT_FOUND);
  end;
end;

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version