Board index » delphi » Adding strings to a file (really need your help )

Adding strings to a file (really need your help )

Its just killing me...
In delphi, is there a way to add strings to a file,
something like savetofile, but instead "add" to a file? Savetofile
overwrits the old file, i need it to just add stings to it...
Could anyone help?
Thanks a lot
 

Re:Adding strings to a file (really need your help )


Quote
tihon wrote in message <1149c601.0202171030.3af06...@posting.google.com>...
>Its just killing me...
>In delphi, is there a way to add strings to a file,
>something like savetofile, but instead "add" to a file? Savetofile
>overwrits the old file, i need it to just add stings to it...

You can load the original contents of the file into a
TStrings object[0], append (or merge in) the new lines,
and write the new contents out.

Or you can open the file in append mode, and write to
it using the original Pascal-style procedures (Assign,
Reset/Rewrite/Append, Write/WriteLn, Close).

Groetjes,
Maarten Wiltink

[0] And please don't try to instantiate TStrings. If
    you were going to, read the miniFAQ first.

Re:Adding strings to a file (really need your help )


TFileStream, THandleStream  ???

On 17 Feb 2002 10:30:02 -0800, ti...@post.com (tihon) wrote:

Quote
>Its just killing me...
>In delphi, is there a way to add strings to a file,
>something like savetofile, but instead "add" to a file? Savetofile
>overwrits the old file, i need it to just add stings to it...
>Could anyone help?
>Thanks a lot

Re:Adding strings to a file (really need your help )


Quote
"tihon" <ti...@post.com> wrote in message

news:1149c601.0202171030.3af06aed@posting.google.com...

Quote
> Its just killing me...
> In delphi, is there a way to add strings to a file,
> something like savetofile, but instead "add" to a file? Savetofile
> overwrits the old file, i need it to just add stings to it...
> Could anyone help?

procedure AddToFile(FileName, Text : string);
var
    f : TextFile;
begin
    AssignFile(f, FileName);
    if FileExists(FileName) then Append(f)
    else Rewrite(f);
    Write(f, Text); // or maybe you want WriteLn()
    CloseFile(f);
end;

It's not the nicest way but it'll help til you learn about TFileStream.

Re:Adding strings to a file (really need your help )


How about using a TFileStream.

Procedure AppendFile( Const Filename, Text : String );
Var
  FileStream : TFileStream;
Begin
  If FileExists( FileName )
  Then FileStream := TFileStream.Create( Filename, fmCreate )
  Else FileStream := TFileStream.Create( Filename, fmOpenReadWrite +
fmShareDenyAll );  //Stops other apps from using the file
  Try
    //This moves the file pointer to the end of the file, quickly.
    FileStream.Seek( 0, soFromEnd ); //Not sure of correct syntax offhand
but it's something like that - lookup the seek command.
    //This should write the text to the file.
    FileStream.Write( Text[ 1 ], Length( Text ) );
  Finally
    FileStream.Free;
  End;
End;

I've not checked the above for accuracy, and you may need to alter it
slightly to get it working the way that you wish.

--
Regards

Colin Dawson
n...@cdawson.tele2.co.uk

Quote
"Smola" <supersmola...@VEinet.hr> wrote in message

news:a4r4ep$ouk$1@sunce.iskon.hr...
Quote

> "tihon" <ti...@post.com> wrote in message
> news:1149c601.0202171030.3af06aed@posting.google.com...
> > Its just killing me...
> > In delphi, is there a way to add strings to a file,
> > something like savetofile, but instead "add" to a file? Savetofile
> > overwrits the old file, i need it to just add stings to it...
> > Could anyone help?

> procedure AddToFile(FileName, Text : string);
> var
>     f : TextFile;
> begin
>     AssignFile(f, FileName);
>     if FileExists(FileName) then Append(f)
>     else Rewrite(f);
>     Write(f, Text); // or maybe you want WriteLn()
>     CloseFile(f);
> end;

> It's not the nicest way but it'll help til you learn about TFileStream.

Re:Adding strings to a file (really need your help )


Thanks very much to all of you!
You gave me all i needed and i learnt a lot!
Feel myself so stupid! ;)
Thanks very much again!

Re:Adding strings to a file (really need your help )


Quote
"tihon" <ti...@post.com> wrote in message

news:1149c601.0202181059.7812a393@posting.google.com...

Quote
> Feel myself so stupid! ;)

1. No reason.
2. Mr. Bjrrge Scther stated a few posts away that he understands nothing
more :-)))

Other Threads