Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-11-02
7
Medium Priority
?
180 Views
Last Modified: 2010-04-05
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
Comment
Question by:reynaerde
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 80 total points
ID: 12472779
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
 
LVL 12

Assisted Solution

by:esoftbg
esoftbg earned 80 total points
ID: 12472861
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 12472874
lbxResult is TListBox;
0
Industry Leaders: 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 6

Assisted Solution

by:vadim_ti
vadim_ti earned 1240 total points
ID: 12472890

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
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12473224
vadim_ti, excellent man !!! :))))
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12492791
download an example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21191477.zip
0
 

Author Comment

by:reynaerde
ID: 12582686
doh!
0

Featured Post

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.

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 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