Solved

write to a file

Posted on 1998-01-14
8
125 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now