Solved

write to a file

Posted on 1998-01-14
8
131 Views
Last Modified: 2010-04-04
Hi!

I need to apply some data to a file in Delphi 3, how do I do that? Code thanks
0
Comment
Question by:hh
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 2

Expert Comment

by:Waldek
ID: 1356600
Some data ? What?
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1356601
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
0
 

Author Comment

by:hh
ID: 1356602
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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:JimBob091197
ID: 1356603
Well, hard-code the file name then, using ZifNab's example:

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

0
 

Author Comment

by:hh
ID: 1356604
but dosent ZifNab's example use a dialog box?
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1356605
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
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1356606
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.
0
 

Accepted Solution

by:
JimL011598 earned 50 total points
ID: 1356607
I prefer to we the following method for write to files from Delphi.

    FileHandle := fileopen(FileName, fmOpenWrite);
    If FileHandle > 0
    then begin
         fileseek(FileHandle, 0, 2);  {position to end of file}
         filewrite(FileHandle, Buffer, length(Buffer)+1);
         fileclose(FileHandle);
         end
    else begin
         { handle error }
         end;
 
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Internet Explorer View Settings Question 15 116
drawing animated level bar based on numbers 3 103
Delphi...Split view - idea? 1 87
Tvertscrollbox like a whatsapp layout 5 34
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question