Solved

Storing Info in a data file

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 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