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

How to Combine Two Path Strings in C++ Builder

How to Combine Two Path Strings in C++ Builder

How can we combine two path strings in C++ Builder? What is Combine Method? What is the Syntax of Combine Method? Is there an Example to Combine Method?

C++ Builder has a lot of specific methods in its SysUtils library that is included in vcl and fmx libraries. Some of these are grouped as a 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 all methods are easy to use and easy to get or set file path strings in that operating system. These can be used with other component properties like FileName property of OpenDialog, SaveDialog components. We can also check drive, files or directories if they are exist or not in that given path. And we can combine two path strings as new path string.

Let’s see how we can use Combine Method to combine two paths on Windows.

What is the Combine method in modern C++ ?

Combine Method (System::IOUtils::TPath::Combine) is a Path Manipulation Routine that combines two paths strings. We can call Combine to obtain a new combined path from two distinct paths. If the second path is absolute, Combine returns it directly; otherwise Combine returns the first path concatenated with the second one.

What is the syntax of the Combine method in modern C++ ?

Here is the Syntax of Combine() Method:

The following table lists the parameters expected by this method:

NameMeaning
Path1The first path. Path1 is used as root for Path2.
Path2The path that is concatenated with Path1.

Note: Combine raises an exception if the given paths contain invalid characters.

As you see Path1, Path2 parameters and returning parameter are UnicodeString which means you can use any word language if your operating system support it.

Six simple examples of using the Combine method in C++

Combine Method returns a UnicodeString, we can check a file and set the result to a bool as in different examples below,

Example 1

Let’s combine two path Strings. The first one has a drive letter with a folder name and the second one has folder names,

and the output form of ShowMessage() will display result path String as below,

as you see we use “\\” to define single “\” in Strings. Combine Method() automatically adds “\” to between two paths if needed.

Example 2

Let’s try Example 1 with a “\” at the end of path1,

and the output form of ShowMessage() will display result path String as below,

as you see we use “\\” to define single “\” in Strings. Combine Method() automatically adds “\” to between two paths if needed.

Example 3

Let’s try Example 1 with a “\” at the end of path1,

and the output form of ShowMessage() will display result path String as below,

as you see Combine Method() automatically combines two paths without adding “\” to between two paths.

Example 4

Let’s try Example 1 with a “\” at the front of path2,

and the output form of ShowMessage() will display result path String as below,

as you see Combine Method() automatically combines two paths without adding “\” to between two paths.

Example 5

and this time the output form of ShowMessage() will display result path String as below,

as you see Combine Method() returns the second one, because both has different paths.

Example 6

and in this example the output form of ShowMessage() will display result path String as below,

as you see Combine Method() returns the second one, because the second includes the first one.

From these examples we can briefly say that if the second parameter has a drive with ‘:’ then it returns the combination of Path1+Path2 or Path1+ Path2 ;

Here is a full example of how to use the Combine method in C++

Here is the full C++ Builder VCL example with the Memo (TMemo) component below,

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++Introduction to C++Language FeatureLearn C++

How To Generate A Random File Name on Windows

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

How To Include Or Exclude Trailing Backslashes On Windows

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

How To Check Characters of File Names and Paths on Windows

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

How to use SameFileName method on Windows

en_USEnglish