Site icon Learn C++

How to Combine Two Path Strings in C++ Builder

How to Combine Two Path Strings in C++ Builder

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 s a Path Manipulation Routines that allows 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 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 drives, files, or directories to see if they exist or not in that given path. And we can combine two path strings as a new path string.

In this post, you’ll learn how to combine two path strings in C++ Builder, what the Combine Method is, what the Syntax of the Combine Method is, and examples of the Combine Method. By learning how to use Combine Method to combine two paths on Windows, it will help you to easily build C++ applications using the C++ IDE.

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:

[crayon-674011029ef3d622742728/]

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,

[crayon-674011029ef49805645716/]

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

[crayon-674011029ef4b137058121/]

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,

[crayon-674011029ef4c209072958/]

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

[crayon-674011029ef55889572776/]

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,

[crayon-674011029ef56345275898/]

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

[crayon-674011029ef5a427260296/]

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,

[crayon-674011029ef5c885936917/]

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

[crayon-674011029ef5d044836358/]

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

Example 5

[crayon-674011029ef5e263905069/]

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

[crayon-674011029ef5f259883068/]

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

Example 6

[crayon-674011029ef60264997326/]

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

[crayon-674011029ef65100983954/]

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,

[crayon-674011029ef66522621331/]

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.

Exit mobile version