Link to home
Start Free TrialLog in
Avatar of hh
hhFlag for Denmark

asked on

write to a file

Hi!

I need to apply some data to a file in Delphi 3, how do I do that? Code thanks
Avatar of Waldek
Waldek

Some data ? What?
You can put everything to a file. What data do you want to write to a file. Here is a basic code example (for a textfile)

var
  F: TextFile;
  S: string;
begin
  if OpenDialog1.Execute then          { Display Open dialog box }
  begin
    AssignFile(F, OpenDialog1.FileName);   { File selected in dialog box } or
    AssignFile(F, 'name of file');
    Rewrite(F);
    S := Edit1.text;
    writeln(F, S);                          { write S to file }
    CloseFile(F);
  end;
end;

Look in the helpfile of delphi. It's all in the System unit
Avatar of hh

ASKER

I dont want a dialog box. The user shall press a button and a line of data should be added to a file. The data shall be automatically added to the file without confirmation from the user.
Well, hard-code the file name then, using ZifNab's example:

AssignFile(F, 'Name of file');
Etc.

Avatar of hh

ASKER

but dosent ZifNab's example use a dialog box?
Yeah, so, take it out.

E.g.  Write "Hello there" to "C:\Test.txt"

procedure WriteHello;
var
  S: string;
  F: TextFile;
begin
  S := 'Hello there';
  AssignFile(F, 'C:\Test.txt');
  Rewrite(F);   // To append, use "Append(F);"
  Writeln(F, S);
  CloseFile(F);
end;

JB
Like in the example (and JimBob explained)

You can use the procedure

AssignFile({filetype}, {string}) in many ways.

eg.
AssignFile(F, 'filename');

Just opens the file with the name 'filename'.

If you want to let the user chose the file, you can sue the OpenFiledialog, because this one returns a string when closed.

AssignFile(F, OpenDialog1.FileName);

Zif.
ASKER CERTIFIED SOLUTION
Avatar of JimL011598
JimL011598

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial