DC Library FAQ Crash Course Delphi Tips Source Code Downloads Links
© Copyright 1999-2019 DelphiLand
Delphi in a Nutshell by Ray Lischner
Get full access to Delphi in a Nutshell and 60K+ other titles, with a free 10-day trial of O'Reilly.
There are also live events, courses curated by job role, and more.
Assign Procedure
Description
The Assign procedure does the same thing as AssignFile , but you should use AssignFile in new code. Assign is a method name that is often used in Delphi, and the two names can result in confusion. Assign is not a real procedure.
Get Delphi in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.
Don’t leave empty-handed
Get Mark Richards’s Software Architecture Patterns ebook to better understand how to design components—and how they should interact.
It’s yours, free.
Check it out now on O’Reilly
Dive in for free with a 10-day trial of the O’Reilly learning platform—then explore all the other resources our members count on to build skills and solve problems every day.
We don't display ads so we rely on your Bitcoin donations to 1KWEk9QaiJb2NwP5YFmR24LyUBa4JyuKqZ
Using resource files with Delphi
Post date: Dec 10, 2009 4:01:26 PM
Create the resource file script.
What follows is a step by step instruction for creating resource files.
Resource files can contain text, html documents, sounds, images etc.
Open a simple text editor, e.g. Notepad and enter the files you want to include in your application.
Every line consist of a resource name , a resource type and the resource .
In the example below support is the resource name , HTML is the resource type and "support.html" is the resource .
support HTML "support.html"
content HTML "content.html"
help HTML "help.html"
bg JPG "bg.jpg"
back BMP "back.bmp"
forward BMP "forward.bmp"
home BMP "home.bmp"
next GIF "next.gif"
Save this file with the .rc extension, e.g. myresource.rc .
Compile the resource file.
Now compile myresource.rc with the resource compiler BRCC32.exe which you will find in your Delphi5\bin\ directory.
BRCC32.exe is a command line utility that has the .rc extension associated with it.
Double-click myresource.rc in Windows Explorer and it will be compiled to myresource.res .
Include the resource file in your project.
Enter a statement like {$R .\resources\myresource.res} in your main unit file.
This will look then something like:
implementation
{$R .\resources\myresource.res}
Assuming that you keep the resource for a project in a subdirectory called resources , (like I always do for clarity).
Loading the resource file
Next you need a procedure to load a named resource into a memorystream
procedure LoadResourceFile(aFile: string ; ms:TMemoryStream);
HResInfo: HRSRC;
HGlobal: THandle;
Buffer, GoodType : pchar;
I: integer;
Ext: string ;
ext:=uppercase(extractfileext(aFile));
ext:=copy(ext,2,length(ext));
if ext='HTM' then ext:='HTML';
Goodtype:=pchar(ext);
aFile:=changefileext(afile,'');
HResInfo := FindResource(HInstance, pchar(aFile), GoodType);
HGlobal := LoadResource(HInstance, HResInfo);
if HGlobal = 0 then
raise EResNotFound.Create('Can''t load resource: '+aFile);
Buffer := LockResource(HGlobal);
ms.WriteBuffer(Buffer[0], SizeOfResource(HInstance, HResInfo));
ms.Seek(0,0);
UnlockResource(HGlobal);
FreeResource(HGlobal);
The TmemoryStream must be created before you call the routine. If you use a lot of resources in your program you can create the variable ms in the FormCreate event and destroy it in the FormDestroy event.
aFile is the name of a resource file to be loaded, without path information.
Personally I always use the method of giving the resource name the same name as the resource file without the extension . This allows for a generic routine like above.
Using the resource file
Finally we want to use the resource file that is now in the TMemoryStream variable ms .
In Delphi many components have a LoadFromStream method that you can use to load the resource.
Suppose we want to load the resource file support.html into a Tstrings .
procedure LoadStringResource;
List:=TStringList.create;
LoadResourceFile('support.html',ms);
Memo1.Lines.LoadFromStream(ms);
In the above Memo1 is a TMemo .
Alternative method
procedure LoadStringResource2;
tmpStream: TResourceStream;
tmpStream := TResourceStream.Create( HInstance, 'support', 'HTML' );
memo1.Lines.LoadFromStream( tmpStream );
tmpStream.Free;
Compiling resources into your application is easy. It can be used to include all the html docs and images that together make up the user interface. These could have included these files as external files, but this way I am sure that they are always there when the program needs them.
They can also be used to create custom install packages.
System.AssignFile
- Up to Parent: System
- 1 Properties
- 2.1 See Also
- 2.2 Code Examples
Description
Associates the name of an external file with a file variable.
Call AssignFile to initialize a file variable in Delphi code. F is a file variable of any file type. FileName is a string-type expression or an expression of type PChar if extended syntax is enabled.
After calling AssignFile , F is associated with the external file until F is closed. All further operations on the file variable F operate on the external file named by FileName.
When the FileName parameter is empty, AssignFile associates F with the standard input or standard output file. If assigned an empty name, after a call to Reset (F), F refers to the standard input file, and after a call to Rewrite (F), F refers to the standard output file.
Do not use AssignFile on a file variable that is already open.
Note: To avoid scope conflicts, AssignFile replaces the Assign procedure that was available in early versions of the Delphi product. However, for backward compatibility, Assign is still available.
- File Input and Output Support
- Delphi Intrinsic Routines
- Code Examples
- FileListBox (Delphi)
- TOpenDialogFileName (Delphi)
- TOpenDialogTitle (Delphi)
- API Documentation
Navigation menu
Personal tools.
- View source
- View history
RAD Studio 10.4 Sydney
- Libraries Reference
RAD Studio 10.4 Libraries
- FireMonkey Unit List
- VCL Unit List
- RTL Unit List
In Other Languages
Previous versions.
- Rio Libraries
- Tokyo Libraries
- Older Versions
- Recent changes
- What links here
- Related changes
- Special pages
- Printable version
- Permanent link
- Page information
- This page was last edited on 10 February 2014, at 03:26.
- Privacy policy
- About RAD Studio API Documentation
- Disclaimers
- Help Feedback ( QP , email )
- Mobile view
IMAGES
VIDEO
COMMENTS
The AssignFile procedure assigns a value to FileHandle for a FileName in preparation for reading or writing to that file. Version 1. Takes a text file variable type as the handle. The file is treated as a textfile when opened. If the file name is an empty string, then file access is made to the console standard input and output streams. Version 2.
Description. Associates the name of an external file with a file variable. Call AssignFile to initialize a file variable in Delphi code. F is a file variable of any file type. FileName is a string-type expression or an expression of type PChar if extended syntax is enabled. After calling AssignFile, F is associated with the external file until ...
It has a lot of utilities for writing to files. e.g. procedure WriteAllText (const Path: string; const Contents: string); overload; static; Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten. answered Oct 20, 2011 at 5:43.
See the example with the Array. // Keyword for the other overloaded Log procedure. F: TextFile; AssignFile(F, LogFile); // Try to append to the file, which succeeds only if the file exists. Append(F); if IOResult <> 0 then. // The file does not exist, so create it. Rewrite(F);
The delphi help files suggest that you use the Pascal routines if you can. Use of the non-native Pascal file variable handlers such as FileOpen is discouraged. These routines map to the Windows API functions and return file handles, not normal Pascal file variables. These are low-level file access routines.
For saving one or more strings to a text file, you code the following steps: Declare a variable of the type TextFile.; Use the command AssignFile to connect the TextFile variable to a physical file on disk. "Open" the text file for writing with the command Rewrite.This means, that a new text file will be created on the disk.
Description. Associates the name of an external file with a file variable. Call Assign to initialize a file variable in Delphi code. F is a file variable of any file type. FileName is a string-type expression or an expression of type PChar if extended syntax is enabled. After calling Assign, F is associated with the external file until F is closed.
Yes. The second parameter of AssignFile has type string. The expression cFileDir + '\' + sFile has type string. FWIW, AssignFile is known as a function rather than a command. Getting on top of terminology like this will help you learn the language more effectively. answered Jun 22, 2015 at 15:05. David Heffernan.
Name Assign Procedure Syntax procedure Assign(var F: File; const FileName: string); procedure Assign(var F: TextFile; const FileName: string); Description The Assign procedure does the same thing as AssignFile, but … - Selection from Delphi in a Nutshell [Book]
Here we are getting a handle to a text file, designated by the TextFile type (binary files are of type File).We ask Delphi to assign a file handle for a file called 'Test.txt' which will be assumed to be in the current directory (as given by the GetCurrentDir routine).: Next, we must open the file using this handle.
Create the resource file script. What follows is a step by step instruction for creating resource files. Resource files can contain text, html documents, sounds, images etc. Open a simple text editor, e.g. Notepad and enter the files you want to include in your application. Every line consist of a resource name, a resource type and the resource.
Regarding your update, assign s.Size to a local variable of type Integer and then use WriteBuffer to save it. In reverse, use ReadBuffer to read into a local variable. If I were you I would write direct to the file and avoid the memory streak. Use the Position property of TStream to seek around the file.
In Delphi können Sie mit AssignFile eine Dateivariable initialisieren. F ist eine Dateivariable eines beliebigen Dateityps. FileName enthält einen Ausdruck des Typs String bzw. PChar (wenn die erweiterte Syntax aktiviert ist). Nach dem Aufruf von AssignFile ist F der externen Datei zugeordnet, bis F wieder geschlossen wird.
You can then check if it has already been assigned to a file with this: IF TTextRec(FilePtr).Handle=0 THEN BEGIN. AssignFile(FilePtr,'File1.txt'); Rewrite(FilePtr) END; When you close it, you must also add this line: CloseFile(FilePtr); TTextRec(FilePtr).Handle:=0; If your FilePtr is a FILE and not a TextFile, replace TTextRec with TFileRec ...
Description. Associates the name of an external file with a file variable. Call AssignFile to initialize a file variable in Delphi code. F is a file variable of any file type. FileName is a string-type expression or an expression of type PChar if extended syntax is enabled. After calling AssignFile, F is associated with the external file until ...
Mastering Picture Loading in Delphi: Learn how to efficiently load and manipulate images using Delphi's powerful Image component. In this tutorial, we delve ...