Solved

Storing Info in a data file

Posted on 2000-02-17
4
131 Views
Last Modified: 2010-05-18
I would like to know how to store some data (that the user enters) into a file, that can be saved, and opened later. I will have different fields for the user to enter what they want. Please help me to do this, or suggest the commands that I should look into more. Thanks
(I have Delphi 5)
0
Comment
Question by:blink_me
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 100 total points
ID: 2533196
mmm, it all depends on what kind of data you want to store. If it are just preferences (like options), I would use an ini-file (if you don't like the registry). For other none-repeting data, I would also mostly use an ini-file. For repeating data, I would use a database.

Here is some info (from delphi help) :

writing to a plain text file :

var F: TextFile;

begin
  AssignFile(F, 'NEWFILE.$$$');
  Rewrite(F);
  Writeln(F, 'Just created file with this text in it...');
  CloseFile(F);
end;

opening of a plain text file :

var

  F: TextFile;
  S: string;
begin
  if OpenDialog1.Execute then          { Display Open dialog box }
  begin
    AssignFile(F, OpenDialog1.FileName);   { File selected in dialog box }
    Reset(F);
    Readln(F, S);                          { Read the first line out of the file }
    Edit1.Text := S;                       { Put string in a TEdit control }
    CloseFile(F);
  end;
end;

using TIniFile :

This example reads the Transfer section of the DELPHI32.INI file into a memo and changes one of the strings in the INI file when Button1 is clicked.  When Button2 is clicked, the DELPHI32.INI file is restored to its initial state, using the values stored in the memo.
Before you run this example, you must add the IniFiles unit to the uses clause of your unit.

Warning:      Do not click button2 before you have clicked button1!

procedure TForm1.Button1Click(Sender: TObject);

var
  DelphiIni: TIniFile;
  begin
  DelphiIni := TIniFile.Create('c:\windows\delphi32.ini');
  Memo1.Clear;
  DelphiIni.ReadSectionValues('Transfer', Memo1.Lines);
  if Memo1.Lines.Values['Title1'] <> 'Picture Painter' then
    DelphiIni.WriteString('Transfer', 'Title1', 'Picture Painter');
  DelphiIni.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);

var
  DelphiIni: TIniFile;
  begin

  DelphiIni := TIniFile.Create('c:\windows\delphi32.ini');

  { if the entry wasn’t there before, delete it now }
  if Memo1.Lines.Values['Title1'] = '' then
    DelphiIni.DeleteKey('Transfer', 'Title1')
  { otherwise, restore the old value }
  else
    DelphiIni.WriteString('Transfer', 'Title1', Memo1.Lines.Values['Title1']);
  DelphiIni.Free;
end;


This example reads strings in the DELPHI32.INI file and displays them on the form.
Before you run this example, you must add the IniFiles unit to the uses clause of your unit.

procedure TForm1.FormActivate(Sender: TObject);

var
  DelphiIni: TIniFile;
begin
  Canvas.TextOut(20, 10, 'VARIOUS DELPHI SETTINGS');
  DelphiIni := TIniFile.Create('Delphi32.Ini');
  with DelphiIni do
  begin
    with Canvas do
    begin
      TextOut(10, 50, 'Editor Font = ' +
        ReadString('Editor', 'FontName', 'ERROR'));
      TextOut(10, 70, 'Search Path = ' +
        ReadString('Library', 'SearchPath', 'ERROR'));
      TextOut(10, 90, 'Component Library = ' +

        ReadString('Library', 'ComponentLibrary', 'ERROR'));
      TextOut(10, 110, 'VBX Directory = ' +
        ReadString('VBX', 'VBXDir', 'ERROR'));
      TextOut(10, 130, 'VBX Unit Directory = ' +
        ReadString('VBX', 'UnitDir', 'ERROR'));
    end;
  end;
DelphiIni.Free;
end;


You can also save records to a file.
(use READ, WRITE, see more info in delphi help)

So, look at helpfile to eg: TIniFile, Read, Write, AssignFile, CloseFile etc..

Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 2533200
Ah forgot some, look also at BlockRead, BlockWrite etc...

var

  FromF, ToF: file;
  NumRead, NumWritten: Integer;
  Buf: array[1..2048] of Char;
begin
  if OpenDialog1.Execute then                               { Display Open dialog box }
  begin
    AssignFile(FromF, OpenDialog1.FileName);
    Reset(FromF, 1);      { Record size = 1 }
    if SaveDialog1.Execute then                              { Display Save dialog box}
    begin
      AssignFile(ToF, SaveDialog1.FileName);      { Open output file }

      Rewrite(ToF, 1);      { Record size = 1 }
      Canvas.TextOut(10, 10, 'Copying ' + IntToStr(FileSize(FromF))
        + ' bytes...');
      repeat
        BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
        BlockWrite(ToF, Buf, NumRead, NumWritten);
      until (NumRead = 0) or (NumWritten <> NumRead);
        CloseFile(FromF);
        CloseFile(ToF);
    end;
  end;
end;
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 2533221
0
 
LVL 3

Expert Comment

by:bryan7
ID: 2533494
listenning
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Firewall issues 6 85
Create a Restore Point In Windows 10 3 102
Delphi XE10 Round Image 2 50
Base1 Encode/Decode 3 34
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

21 Experts available now in Live!

Get 1:1 Help Now