Solved

Writing and Reading a File

Posted on 2000-05-09
9
185 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 100
Create Database on Android via Delphi dbExpress 3 90
Delphi XE10 Round Image 2 126
DBCtrlGrid, Delphi, Scroll 8 10
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 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