Solved

write to a file

Posted on 1998-01-14
8
130 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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