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:
1 2 3 |
UnicodeString __fastcall GetRandomFileName(); |
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:
1 2 3 |
UnicodeString __fastcall GetTempFileName(); |
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:
1 2 3 |
UnicodeString __fastcall GetGUIDFileName(const bool UseSeparator = false); |
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,
1 2 3 4 5 6 7 8 9 10 |
String fname1= TPath::GetRandomFileName(); ShowMessage( fname1 ) ; String fname2 = TPath::GetTempFileName(); ShowMessage( fname2 ) ; String fname3 = TPath::GetGUIDFileName(); ShowMessage( fname3 ) ; |
and the outputs will be as follows respectively,
1 2 3 4 5 |
c3ld53Et.Bq3 C:\Users\ata\AppData\Local\Temp\tmp22D.tmp C56B164910334081BA8ED28320140F7A |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <vcl.h> #include <System.IOutils.hpp> #pragma hdrstop #include "Include_Exclude_Path_Delimiters_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { String path1, path2, path, fname; fname= TPath::GetRandomFileName(); Memo1->Lines->Add( L"GetRandomFileName: " + fname ) ; path = TPath::GetFullPath(fname); Memo1->Lines->Add( L"Full Path Random File Name: " + path ) ; fname = TPath::GetTempFileName(); Memo1->Lines->Add( L"GetTempFileName: " + fname ) ; fname = TPath::GetGUIDFileName(); Memo1->Lines->Add( L"GetGUIDFileName: " + fname ) ; path = TPath::GetFullPath(fname); Memo1->Lines->Add( L"Full GUID File Name: " + path ) ; } |
and the output will be as follows respectively,
1 2 3 4 5 6 7 |
GetRandomFileName: G0FBymX6.cPx Full Path Random File Name: D:\#PROJECTS\#LearnCPlusPlus.org\#Path Manipulation Routines\Include Exclude Path Delimeters\Win32\Debug\G0FBymX6.cPx GetTempFileName: C:\Users\ata\AppData\Local\Temp\tmp8402.tmp GetGUIDFileName: 6080275648894A59A88C7191A4E46050 Full GUID File Name: D:\#PROJECTS\#LearnCPlusPlus.org\#Path Manipulation Routines\Include Exclude Path Delimeters\Win32\Debug\6080275648894A59A88C7191A4E46050 |
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition