22

I have a TXT file containing about 10,000 lines of text. I want to display these lines in a TMemo. But I don't want to distribute that TXT file my program. How do I integrate it into my EXE file as a resource WITHIUT using stringtable {} because this requires an identifier for each line (so I will have to add 10000 identifiers).

_

I have Delphi XE

Gabriel
  • 20,797
  • 27
  • 159
  • 293

2 Answers2

41

I usually create an RC file (which is basically an text file) for this kind of resources, then add line like

MyText RCDATA ..\resources\filename.txt

for each file / resource into the RC file. If the RC file is part of the project it will be compiled (to res) and linked into exe. To use the resource I usually use TResourceStream, ie

var ResStream: TResourceStream;
ResStream := TResourceStream.Create(hInstance, 'MyText', RT_RCDATA);

BTW if the RC file wasn't added to the project automatically when you created it in the IDE then add line like

{$R 'myExtraRes.res' 'myExtraRes.RC'}

into the project file, right after the uses list.

ain
  • 22,394
  • 3
  • 54
  • 74
  • 4
    Note that modern Delphi versions now have an integrated Resource Manager in the IDE so you do not need to use .rc files as much anymore. – Remy Lebeau Jan 21 '12 at 02:42
  • 1
    Could you provide an answer indicating how to do it without .rc files? – Toby Allen Dec 16 '12 at 15:40
  • See Remy's answer for one way. There is also some components which are designed for that job, those save the data into DFM... but what's the problem with using RC file? – ain Dec 16 '12 at 18:01
  • If you don't want to use the IDE's Resource Manager, the above steps still work great. You do have to add the `.rc` file to the Delphi project though (as in Project > Add to Project, or drag it into the Project Manager in newer Delphi versions (since about 2006 probably)). (What? You aren't still using Delphi 7?) – Reversed Engineer Jul 20 '17 at 11:42
  • With XE4, the directive `{$R 'myExtraRes.res' 'myExtraRes.RC'}` doesn't compile the `.rc` file into `.res` file when compiling/building the EXE, not sure why, had to use `BCC32` in the command line instead... – Edwin Yip Oct 21 '20 at 14:47
4

Since you want to display the text in a TMemo, you could just copy/paste the text directly into the TMemo.Lines property editor at design-time. The text will be included in the owning Form/Frame's DFM resource at compile-time and loaded into the TMemo automatically at run-time for you.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    I know, but this is not what I wanted. The TMemo thingy was only as an example (and a test that the resource string is stored correctly). – Gabriel Jan 22 '12 at 17:30