C++Introduction to C++Language FeatureLearn C++

How To Generate A Random File Name on Windows

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.IOUtilsSystem.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.

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:

NameMeaning
UseSeparatorIndicates 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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++

What Is The Priority Queue (std::priority_queue) In Modern C++?

C++C++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?