Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to save data to file

Posted on 2005-05-11
11
Medium Priority
?
296 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:liorb
11 Comments
 
LVL 8

Expert Comment

by:Cesario
ID: 13975846
go to torry.net
http://www.torry.net/pages.php?id=96

and take a look at this component
Persistent Form v.2.6
0
 
LVL 11

Expert Comment

by:calinutz
ID: 13975880
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
0
 
LVL 1

Author Comment

by:liorb
ID: 13975900
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.
0
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!

 
LVL 11

Expert Comment

by:calinutz
ID: 13975911
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
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13975926
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');

0
 
LVL 11

Expert Comment

by:calinutz
ID: 13975988
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
0
 
LVL 11

Expert Comment

by:calinutz
ID: 13975993
//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.
0
 
LVL 11

Expert Comment

by:calinutz
ID: 13976005
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
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 450 total points
ID: 13976082
0
 
LVL 5

Expert Comment

by:Gwena
ID: 13980281
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.
0
 
LVL 2

Expert Comment

by:dKasipovic
ID: 13982561
i suggest using XML file format... some good xml parser will do the job...
hope i helped...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

577 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