Board index » delphi » D3 How to open a file, write in it then close

D3 How to open a file, write in it then close

Hi,

I've got a problem with writing to a file. I use an edit field for the
information which should appear in a bin-file (e.g. *.exe) - In VB it's
easy:

.........

    Dim name1, filename, As Variant
    Dim patchtext As String
    Dim patchinfo As String

...

        If (FileLen(name1) = 5318416) Then
            patchinfo = Text4.Text
            Open name1 For Binary As 1
            Put 1, Val(226193), patchtext
            Put 1, Val(5275597), patchinfo
            Close 1
..............

How can I do this with delphi?

Can someone help me please...

many Thanks

Bye

Ralf

 

Re:D3 How to open a file, write in it then close


Ralf,

you could use filestreams, e.g.

 Var
   fs: TFileStream;
   patchtext, patchinfo: String;
   offset: LongInt;

 Begin
   ..assign values to patchtext etc.

   fs:= TFileString.Create( exefilename,
                            fmOpenReadWrite or fmShareExclusive );
   try
     If fs.Size = expectedFilesize Then Begin
       fs.Position := offset;
       fs.Write( patchtext[1], Length(patchtext)+1 );
         // writes string + following #0
       .... etc.  
     End
   finally
     fs.free;
   end;  

Note that you cannot write to a running EXE in Win32, the file is kept open
by the OS as long as the program runs and cannot be written to.

Peter Below (TeamB)  100113.1...@compuserve.com)

Other Threads