C++ Builder has a lot of specific methods in its SysUtils library that are included in the VCL and FMX libraries. Some of these are grouped as Path Manipulation Routines that allow users to edit, extract, get and set drive name, directory name, file name and file extensions. These methods are combined in Vcl.FileCtrl, System.IOUtils, System.SysUtils libraries. These methods are easy to use and with them it is easy to get or set file path strings in that operating system. These methods can be used with other component properties like FileName property of OpenDialog, SaveDialog components. We can also check drives, files, or directories to see if they exist or not on a given path. We can include and exclude path delimiters on Windows.
In this post, you’ll learn how to generate a random file name, a unique temporary file name, a GUID file name, and what the GetRandomFileName, GetTempFileName, and GetGUIDFileName methods are. By learning how to generate a random file on windows, and how to compile c++ in windows. It will help you to easily build C++ applications.
Table of Contents
What is the C++ GetRandomFileName method ?
The GetRandomFileName Method (System::IOUtils::TPath::GetRandomFileName) is an IOUtils Method that generates a new random file name. GetRandomFileName does not guarantee a unique file name across the drives or network. If an absolutely, definitely unique file name under all circumstances is desired, use the GetGUIDFileName method instead. To generate a real uniquely-named temporary file, use the GetTempFileName method.
What is the syntax of GetRandomFileName method ?
Here is the GetRandomFileName Method:
What is the C++ GetTempFileName method ?
The GetTempFileName Method (System::IOUtils::TPath:: GetTempFileName ) is an IOUtils Method that generates a unique temporary file name. We can call GetTempFileName to generate a new uniquely-named temporary file. GetTempFileName actually creates a zero-sized file in a temporary location and returns its name. The caller must delete the file after it is not used anymore. Note: GetTempFileName raises an exception if the user has no access to the system’s temporary directory.
On Linux, it creates a new file which name is based on GUID (Globally Unique IDentifier) in the following format: ‘File_%8x%4x%4x%16x_tmp’:
- %8x stands for GUID.D1
- %4x stands for GUID.D2
- %4x stands for GUID.D3
- %16x stands for GUID.D4
What is the syntax of GetTempFileName method ?
Here is the GetTempFileName Method:
What is the C++ GetGUIDFileName method ?
GetGUIDFileName Method (System::IOUtils::TPath::GetGUIDFileName ) is a IOUtils Method that Generates a unique temporary file. We can call GetGUIDFileName to generate a new GUID (Globally Unique IDentifier) suitable as a unique name for a file or directory. GetGUIDFileName only generates a file name and does not create a real file.
The following table lists the parameters expected by this method:
Name | Meaning |
---|---|
UseSeparator | Indicates whether the GUID separator char (the minus sign) is preserved in the generated name. |
What is the syntax of GetGUIDFileName method ?
Here is the GetGUIDFileName Method:
What is a simple example of using the C++ GetRandomFileName, GetTempFileName, GetGUIDFileName methods?
GetRandomFileName, GetTempFileName, GetGuidFileName methods return UnicodeString. We can use them by including System.IOutils.hpp and TPath:: as given below,
and the outputs will be as follows respectively,
Here is a full example of how to use the C++ GetRandomFileName, GetTempFileName, GetGUIDFileName methods
Here is the full C++ Builder VCL example with a Memo component,
and the output will be as follows respectively,
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.