Solved

Storing Info in a data file

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

Technology Partners: 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

Title # Comments Views Activity
Delphi: how to implement a User Shortcut mapper? 1 132
Run video youtube webbrowse 10 73
How to build JSON File in Delphi 6 3 66
Firemonkey allowing RTL on android 6 56
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

680 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