Link to home
Start Free TrialLog in
Avatar of liorb
liorb

asked on

How to save data to file

Hello all
I have a FORM1 in which I have some EDIT boxes.
Some of the EDIT boxes hold a STRING data (like name and address) and some holds INTEGER data(like age and phone number).
On another form (FORM2) I have a COMBOBOX from which I want to see (by rolling down) the list of names select one and get some of the saved data that belong to the particular selected name.

I need to know:
A) how to save all of this data onto a .txt or .sav file and not deleting previous data which was allready saved.
B) how to read some and all of the data from the saved file.

Lior
Avatar of Cesario Lababidi
Cesario Lababidi
Flag of Germany image

go to torry.net
http://www.torry.net/pages.php?id=96

and take a look at this component
Persistent Form v.2.6
You seem to need a database ... In case you need to preserve more information somewhere, not only the last data that was entered in the form.
So I suggest either look for a free database (MySQL,SQLite,MiniSQL) or maybe even MSAccess (not free).
You could also use xml format to save and load data.
I suggest that also on your form2 the combobox that holds the names to associate the name with the xml filenames, so everytime you change the name in the combobox you may trigger the refresh from the respective xml file.
There are more than one approach to your question... So you must be more specific
Cheers
Avatar of liorb
liorb

ASKER

with DB I allready know how to work.
What I want is to creat A .txt file or .sav file and read and write data to and from it.
of course you can solve this using txt files also... Use the ini files in delphi and generate ini files named by the names used in Form2 and when you change the name in form2 you can find out the name of the file that you must load expl...
f:='C:\AppFolder\Data\'+ComboBox1.Text+'.ini'

//read from IniFile (from delphi Help)
procedure TForm1.FormCreate(Sender: TObject);
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create( ChangeFileExt( Application.ExeName, '.INI' ) );
  try
    Top     := Ini.ReadInteger( 'Form', 'Top', 100 );
    Left    := Ini.ReadInteger( 'Form', 'Left', 100 );
    Caption := Ini.ReadString( 'Form', 'Caption', 'New Form' );
    if Ini.ReadBool( 'Form', 'InitMax', false ) then
      WindowState = wsMaximized
    else

      WindowState = wsNormal;
  finally
    TIniFile.Free;
  end;
end;

//Write from INI file (delphi Help)
procedure TForm1.FormClose(Sender: TObject; var Action TCloseAction)
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create( ChangeFileExt( Application.ExeName, '.INI' ) );
  try
    Ini.WriteInteger( 'Form', 'Top', Top);
    Ini.WriteInteger( 'Form', 'Left', Left);
    Ini.WriteString( 'Form', 'Caption', Caption );
    Ini.WriteBool( 'Form', 'InitMax', WindowState = wsMaximized );

  finally
    TIniFile.Free;
  end;
end;

You could addapt this to suit your needs
Cheers
A quick way if you just wanted to do a text file would be to use a stringlist then. Just convert any integer values to string and read them back as integers later.
Just give each line you save an identifier, maybe even like XML tags
<EDIT1>This Text<\EDIT1>
Then you could write some simple text routines to read this data. This is about as quick and easy as you could do.

sl: TStringList;

// load info
sl.Create;
sl.LoadFromFile(c:\FormSettings.txt');

// save settings
sl.Add('Setting1');
sl.Add('Setting2');
sl.Add('Setting3');
sl.SaveToFile('c:\FormSettings.txt');

Example:
//This is the form

object Form1: TForm1
  Left = 192
  Top = 107
  Width = 278
  Height = 193
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 132
    Top = 12
    Width = 28
    Height = 13
    Caption = 'Name'
  end
  object Label2: TLabel
    Left = 132
    Top = 52
    Width = 38
    Height = 13
    Caption = 'Address'
  end
  object Label3: TLabel
    Left = 132
    Top = 92
    Width = 30
    Height = 13
    Caption = 'Bonus'
  end
  object Button1: TButton
    Left = 16
    Top = 12
    Width = 75
    Height = 25
    Caption = 'Save'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Edit1: TEdit
    Left = 132
    Top = 24
    Width = 121
    Height = 21
    TabOrder = 1
    Text = 'calin'
  end
  object Edit2: TEdit
    Left = 132
    Top = 64
    Width = 121
    Height = 21
    TabOrder = 2
    Text = 'Romania'
  end
  object Edit3: TEdit
    Left = 132
    Top = 104
    Width = 121
    Height = 21
    TabOrder = 3
    Text = '20000'
  end
  object Button2: TButton
    Left = 16
    Top = 104
    Width = 75
    Height = 25
    Caption = 'Load'
    TabOrder = 4
    OnClick = Button2Click
  end
end
//And the code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,IniFiles;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Edit2: TEdit;
    Label2: TLabel;
    Edit3: TEdit;
    Label3: TLabel;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  Ini: TIniFile;
begin
If DirectoryExists('C:\Data')=false then CreateDir('C:\Data');
  Ini := TIniFile.Create( 'C:\Data\'+Edit1.Text+'.ini' );
  try
    Ini.WriteString( 'Form1', 'Edit1', Edit1.Text);
    Ini.WriteString( 'Form1', 'Edit2', Edit2.Text);
    Ini.WriteString( 'Form1', 'Edit3', Edit3.Text );
  finally
    Ini.Free;
  end;
  Edit1.Text:='';
  Edit2.Text:='';
  Edit3.Text:='';
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create( 'C:\Data\'+Edit1.Text+'.ini' );
  try
    Edit2.Text     := Ini.ReadString( 'Form1', 'Edit2', '' );
    Edit3.Text    := IntToStr(Ini.ReadInteger( 'Form1', 'Edit3', 0 ));
  finally
    Ini.Free;
  end;
end;



end.
The above example uses only one form and the same Edit1 for saving and loading from the inifiles.
So at first you save the filled Edits in the ini file that is named after Edit1.text

And When you want to load the data saved under a specific name just write the name in the Edit1 and Press Load Button.
You need to adjust this example to what you need exactly
Cheers
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
On my website is a simple demo...   http://www.geocities.com/gacarpenter386/datafiles.zip
(you may need to right-click the link and choose 'save target as' to download)

It shows how to store strings and numbers into an external datafile by giving the data variable names.
Of course you can store,delete,alter,replace anything at all and not just numbers and strings.
If the datafile became really large this method might become cumbersome... but several MB should work
fine...just test it out with the amount of data you anticipate using and see if anything blows up :-)

There is a compiled demo exe inside the zip along with full source.

Here is the readme from this old demo :-)

------------------------------------------------------------
This demo shows how to create a data file and store
numbers and strings into it.

You store the data using variable names and you can
alter the data stored under a name by saving new data
over it.

Numbers are stored as strings in the data file...this
makes looking at the data files easy as you can read the
values in a text editor. Conversions are easily made
to/from string and numerical using the delphi functions

IntToStr(x);
StrToInt(S);

FloatToStr(X);
StrToFloat(S);

You are not limited to storing text or numbers... any data
can be stored and retrieved just as easily...images,exe files
...anything. Code is included in ExeMod to handle any data
that needs to be in a stream for easy use...like a jpg.

There are other routines that make handling the data file
much like a dir on a hd... getting a listing of the variable
names in a data file, the size of the data elements and
the number of elements is easy.


To run the demo the first thing you need to do is create
a data file to hold your data... I chose to name the file
MyData.dat and to place it in the dir where the exe resides.
You can of course create any number of files and store them
anywhere on disk.
(you can also append the data to existing files like dll's
or exe files..even the running exe if you wish)

To create MyData.dat just click the button [Create MyData.dat]

Now you can choose some variable names and some strings/numbers
to store in MyData.dat under those names... you can then simply
click [Display string] or [Display number] to display the values.

-----------
This demo uses my ExeMod.pas unit becuse I have a lot of useful
data and file handling code in this unit... you can if you wish
pull out the routines and use them without the unit or simply
include the unit in any program you write (it is freeware for all uses)

I have disabled the CD handling code in the included version of ExeMod
since for these purposes it is not useful.
i suggest using XML file format... some good xml parser will do the job...
hope i helped...