Solved

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

Posted on 2004-11-02
176 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
Question by:reynaerde
    7 Comments
     
    LVL 12

    Accepted Solution

    by:
    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
    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
    lbxResult is TListBox;
    0
     
    LVL 6

    Assisted Solution

    by:vadim_ti

    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
    vadim_ti, excellent man !!! :))))
    0
     
    LVL 12

    Expert Comment

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

    Author Comment

    by:reynaerde
    doh!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    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…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now