[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

Writing and Reading a File

I have two routines to save and load the data records associated with some treenodes.  I can't seem to get it to work.  Here is what I have.  Basically, I want to Delete the file if it already exists or at least make sure it is empty before I save.  

procedure SaveData();
var k:integer;
    TreeNode:TTreeNode;
    DataFile : File of Node;
    tempRec : ctrlPtr;
Begin
        if FileExists('c:\TreeData.dat') then
           DeleteFile('c:\TreeData.dat');
        FileCreate('c:\TreeData.dat');
        AssignFile(DataFile,'C:\TreeData.dat');
        reset(DataFile);
        CloseFile(Datafile);
        For k := 0 to frmForms.tvItems.Items.Count - 1 do begin
          TreeNode := frmForms.tvItems.Items[k];
           if TreeNode.Level = 1 then begin
                AssignFile(DataFile,'C:\TreeData.dat');
                tempRec := TreeNode.Data;
                Reset(DataFile);
                Seek(DataFile, FileSize(DataFile));
                Write(DataFile,tempRec^);
                CloseFile(DataFile);
           end;
        end;
End;

procedure LoadData();
var k:integer;
    TreeNode:TTreeNode;
    DataFile : File of Node;
    tempRec : ctrlPtr;
    count:integer;
Begin
        count := 0;


        For k := 0 to frmForms.tvItems.Items.Count - 1 do begin
          TreeNode := frmForms.tvItems.Items[k];
           if TreeNode.Level = 1 then begin
                New(tempRec);
                AssignFile(DataFile,'C:\TreeData.dat');
                Reset(DataFile);
                Seek(DataFile, SizeOf(node) * count);
                Read(DataFile,tempRec^);
                TreeNode.Data := tempRec;
                Count := Count + 1;
                CloseFile(DataFile);
           end;
        end;
End;
0
Vendi
Asked:
Vendi
  • 5
  • 4
1 Solution
 
MotazCommented:
This is the updated version of your SaveData procedure:
-----------------

procedure SaveData;
var k:integer;
    TreeNode: TTreeNode;
    DataFile : file of Node;
    TempRec : ctrlPtr;
begin
  AssignFile(DataFile,'C:\TreeData.dat');
  Rewrite(DataFile);  // Rewrite overwrites file if exists
  for k := 0 to frmForms.tvItems.Items.Count - 1 do
  begin
    TreeNode:= frmForms.tvItems.Items[k];
    TempRec := TreeNode.Data;
    Write(DataFile, TempRec^);
    CloseFile(DataFile);
  end;
end;


LoadData is comming soon..
0
 
MotazCommented:
LoadData procedure:
-------------
procedure LoadData;
var
  k: Integer;
  TreeNode:TTreeNode;
  DataFile : file of Node;
  tempRec : CtrlPtr;
begin
  AssignFile(DataFile,'C:\TreeData.dat');
  FileMode:= 0; // Read only
  Reset(DataFile);
  for k:= 0 to frmForms.tvItems.Items.Count - 1 do
  begin
    TreeNode := frmForms.tvItems.Items[k];
    New(TempRec);
    Read(DataFile, TempRec^);
    TreeNode.Data:= TempRec;
  end;
  CloseFile(DataFile);
end;
--------

I hope it works,
If you need any help please let me know

Motaz
www.geocities.com/motaz1
0
 
VendiAuthor Commented:
In the SaveData, should the CloseFile be outside the for loop?  I will try this tomorrow and get back to you.  Thanks!!
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!

 
MotazCommented:
Yes, sure, sorry, it SHOULD be out side of the loop.

Motaz
0
 
VendiAuthor Commented:
procedure SaveData;
var k:integer;
    TreeNode: TTreeNode;
    DataFile : file of Node;
    TempRec : ctrlPtr;
begin
  AssignFile(DataFile,'C:\TreeData.dat');
  Rewrite(DataFile);  // Rewrite overwrites file if exists
  for k := 0 to frmForms.tvItems.Items.Count - 1 do
  begin
    TreeNode:= frmForms.tvItems.Items[k];
    if TreeNode.Level = 1 then begin
      TempRec := TreeNode.Data;
      Write(DataFile, TempRec^);
    end;
  end;
     CloseFile(DataFile);
end;




procedure LoadData;
var
  k: Integer;
  TreeNode:TTreeNode;
  DataFile : file of Node;
  tempRec : CtrlPtr;
begin
  AssignFile(DataFile,'C:\TreeData.dat');
  FileMode:= 0; // Read only
  Reset(DataFile);
  for k:= 0 to frmForms.tvItems.Items.Count - 1 do
  begin
    TreeNode := frmForms.tvItems.Items[k];
    if TreeNode.Level = 1 then begin
      New(TempRec);
      Read(DataFile, TempRec^);
      TreeNode.Data:= TempRec;
    end;
  end;
  CloseFile(DataFile);
end;
0
 
VendiAuthor Commented:
I had to add back in my check for the level of the tree node.
0
 
VendiAuthor Commented:
Adjusted points from 50 to 75
0
 
VendiAuthor Commented:
Thanks a bunch Motaz!  
0
 
MotazCommented:
Welcome any time Vendi ;-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

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