Solved

Storing Info in a data file

Posted on 2000-02-17
4
137 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
[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
  • 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

Industry Leaders: 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!

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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