Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to store and retrieve a text file in / from a resource Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Files Operation
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Tomas Rutkauskas

I am trying to work out how to store approximately 1000 lines of text (3 columns, 
30 chars each) inside an application. I do not want to have any .INI entries and 
would prefer not to use an external file.


You can start with one and embed it into a resource. Let's assume you have the data 
in a normal textfile. Make a resource script file (filedata.rc) with a line like

FILEDATA RCDATA filedata.txt

Add this RC file to your Delphi 5 project group. This automatically gets you a $R 
line for it in the project DPR file and the resource is compiled for you when you 
build the project. Now, how to get at the data at runtime? Each line is a fixed 
length of 90 characters. So the file could be represented as an array of records of 
this type:

  TFileData = packed record
    col1, col2, col3: array[1..30] of Char;
    crlf: array[1..2] of Char;
  PFileData = ^TFileData;

Let's use a class to regulate access to the data. I assume you only need to read 
it, so there is no need to copy it from the resource.

1   type
2     TDatahandler = class
3     private
4       FData: TList;
5       FResHandle: THandle;
6       function GetRecord(index: Integer): TFileData;
7       procedure GetRecordCount: Integer;
8       procedure InitDatalist;
9     public
10      constructor Create;
11      destructor Destroy; override;
12      property Records[index: Integer]: TFileData read GetRecord;
13      property Recordcount: Integer read GetRecordcount;
14    end;
16  implementation
18  constructor TDatahandler.Create;
19  begin
20    inherited;
21    FData := TList.Create;
22    InitDatalist;
23  end;
25  destructor TDatahandler.Destroy;
26  begin
27    Fdata.Free;
28    if FResHandle <> 0 then
29    begin
30      UnlockResource(FResHandle);
31      FreeResource(FResHandle);
32    end;
33    inherited;
34  end;
36  function TDatahandler.GetRecord(index: Integer): TFileData;
37  begin
38    Result := PFileData(FData[i])^;
39  end;
41  procedure TDatahandler.GetRecordCount: Integer;
42  begin
43    Result := FData.Count;
44  end;
46  procedure TDatahandler.InitDatalist;
47  var
48    dHandle: THandle;
49    pData: PFileData;
50    numRecords, i: Integer;
51  begin
52    pData := nil;
53    dHandle := FindResource(hInstance, 'FILEDATA', RT_RCDATA);
54    if dHandle <> 0 then
55    begin
56      numRecord := SizeofResource(hInstance, dHandle) div Sizeof(TFiledata);
57      FResHandle := LoadResource(hInstance, dHandle);
58      if FResHandle <> 0 then
59      begin
60        pData := LockResource(dHandle);
61        if pData <> nil then
62        begin
63          FData.Capacity := NumRecords;
64          for i := 1 to Numrecords do
65          begin
66            FData.Add(pData);
67            Inc(pData);
68          end;
69        end
70        else
71          raise Exception.Create('Lock failed');
72      end
73      else
74        raise Exception.Create('Load failed');
75    end
76    else
77      raise Exception.Create('Resource not found');
78  end;

You can add a method to sort the FData list, for example, or a filter method that would populate another TList with pointer to records that match a set of criteria.

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC