?
Solved

write to a file

Posted on 1998-01-14
8
Medium Priority
?
136 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 150 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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