Solved

Writing and Reading a File

Posted on 2000-05-09
9
183 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes, sure, sorry, it SHOULD be out side of the loop.

Motaz
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:Vendi
Comment Utility
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
Comment Utility
I had to add back in my check for the level of the tree node.
0
 
LVL 1

Author Comment

by:Vendi
Comment Utility
Adjusted points from 50 to 75
0
 
LVL 1

Author Comment

by:Vendi
Comment Utility
Thanks a bunch Motaz!  
0
 
LVL 7

Expert Comment

by:Motaz
Comment Utility
Welcome any time Vendi ;-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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