Solved

write to a file

Posted on 1998-01-14
8
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

691 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