[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

File handling: Using records to read and write to/from file

Say I have the following:

TLatticeMetaDataRecord = Record
  Dimensie1, Dimensie2, lifetime : word;
end;

AssignFile(MetaData, 'LatticeMetaData.dat');
ReWrite(MetaData);

tempMetaRecord.Dimensie1 := 700;
tempMetaRecord.Dimensie2 := 700;
tempMetaRecord.lifetime := 100;

Write(MetaData, tempMetaRecord);
CloseFile(MetaData);


AssignFile(MetaData, '..\LatticeMetaData.dat');
FileMode := fmOpenRead;
Reset(MetaData);
read(MetaData, MetaRecord);
CloseFile(MetaData);

Why is it that I don't get the expected results(->tempMetaRecord == MetaRecord values) , i.e. when I look at the MetaRecord it gives me Dimensie1 = 700, Dimensie2 = 0, lifetime = 700..
Is it something in the file opening procedure I'm mis-using?
0
reynaerde
Asked:
reynaerde
3 Solutions
 
Ivanov_GCommented:
just few clarifications:
ReWrite(MetaData); - cleate empty file for Write operations. I.e. After Write method - you can have 1 record on first position.
Reset(MetaData); - open file for reading and place the file cursor on the first record.
read(MetaData, MetaRecord); - read the record where the file cursor is...

You receive totally different results, which makes me think that you are positioned on WRONG record...

Take a look also at:
Append - open the file for appending records after tha last record
Seek - position the records on a spefied record number
FileSize - returns the number of records in a file
0
 
esoftbgCommented:
This code works fine:

procedure TForm1.Button1Click(Sender: TObject);
var
  MetaData:      file of TLatticeMetaDataRecord;
  tempMetaRecord:TLatticeMetaDataRecord;
begin
  AssignFile(MetaData, 'LatticeMetaData.dat');
  ReWrite(MetaData);

  tempMetaRecord.Dimensie1 := 700;
  tempMetaRecord.Dimensie2 := 700;
  tempMetaRecord.lifetime := 100;

  Write(MetaData, tempMetaRecord);
  CloseFile(MetaData);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  MetaData:      file of TLatticeMetaDataRecord;
  MetaRecord:    TLatticeMetaDataRecord;
begin
  AssignFile(MetaData, 'LatticeMetaData.dat');
  FileMode := fmOpenRead;
  Reset(MetaData);
  read(MetaData, MetaRecord);
  CloseFile(MetaData);
  lbxResult.Clear;
  lbxResult.Items.Add('Dimensie1 ' + IntToStr(MetaRecord.Dimensie1));
  lbxResult.Items.Add('Dimensie2 ' + IntToStr(MetaRecord.Dimensie2));
  lbxResult.Items.Add('lifetime  ' + IntToStr(MetaRecord.lifetime));
end;
0
 
esoftbgCommented:
lbxResult is TListBox;
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
vadim_tiCommented:

AssignFile(MetaData, 'LatticeMetaData.dat');
ReWrite(MetaData);

AssignFile(MetaData, '..\LatticeMetaData.dat');
FileMode := fmOpenRead;
Reset(MetaData);

it is looking as 2 different files,
'LatticeMetaData.dat' and '..\LatticeMetaData.dat'
so is nothing strange you have different results
0
 
Ivanov_GCommented:
vadim_ti, excellent man !!! :))))
0
 
esoftbgCommented:
download an example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21191477.zip
0
 
reynaerdeAuthor Commented:
doh!
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now