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

x
?
Solved

Writing and Reading a File

Posted on 2000-05-09
9
Medium Priority
?
193 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 7

Accepted Solution

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

Motaz
0
 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

618 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