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

How To Generate A Random File Name on Windows

How can I generate a random file name? Can I generate a unique temporary file? Can I generate a GUID file name? What is the GetRandomFileName method? What is the GetTempFileName method? What is the GetGUIDFileName method?

C++ Builder has a lot of specific methods in its SysUtils library that are included in VCL and FMX libraries. Some of these are grouped as Path Manipulation Routines that allows user 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 are exist or not on a given path. We can include and exclude path delimiters on Windows.

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.

close

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++ComponentsDatabaseIntroduction to C++Language FeatureLearn C++

Get To Know The Powerful C++ Data Bindings In Windows Apps

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

How to Create a New Windows FMX Static Library In C++

C++C++11C++14C++17Introduction to C++Learn C++

How To Use C++ front() And back() Methods Of Vectors

C++Generic ProgrammingIntroduction to C++Language FeatureLearn C++

5 Things You Need To Know About Optimization In C++ Builder

jaJapanese