Solved

Storing Info in a data file

Posted on 2000-02-17
4
132 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

948 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