Solved

Writing and Reading a File

Posted on 2000-05-09
9
184 Views
Last Modified: 2010-04-04
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
Comment
Question by:Vendi
  • 5
  • 4
9 Comments
 
LVL 7

Expert Comment

by:Motaz
ID: 2795482
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
 
LVL 7

Expert Comment

by:Motaz
ID: 2795489
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
 
LVL 1

Author Comment

by:Vendi
ID: 2798382
In the SaveData, should the CloseFile be outside the for loop?  I will try this tomorrow and get back to you.  Thanks!!
0
 
LVL 7

Accepted Solution

by:
Motaz earned 75 total points
ID: 2799490
Yes, sure, sorry, it SHOULD be out side of the loop.

Motaz
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:Vendi
ID: 2800386
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
 
LVL 1

Author Comment

by:Vendi
ID: 2800391
I had to add back in my check for the level of the tree node.
0
 
LVL 1

Author Comment

by:Vendi
ID: 2800397
Adjusted points from 50 to 75
0
 
LVL 1

Author Comment

by:Vendi
ID: 2800398
Thanks a bunch Motaz!  
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2806559
Welcome any time Vendi ;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Thread safe  opinion 7 126
Working this component WebCopy with proxy, help 3 76
how to center only a line in richedit? 4 52
control image tags in a string ? 12 110
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 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

17 Experts available now in Live!

Get 1:1 Help Now