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.IOUtils, System.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.
Table of Contents
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:
1 2 3 |
UnicodeString __fastcall Combine(const System::UnicodeString Path1, const System::UnicodeString Path2); |
The following table lists the parameters expected by this method:
Name | Meaning |
---|---|
Path1 | The first path. Path1 is used as root for Path2. |
Path2 | The 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,
1 2 3 4 5 6 7 |
String path1 = L"D:\\FirstFolder"; String path2 = L"SecondFolder\\SubFolder"; String path = TPath::Combine( path1, path2); ShowMessage( path ); |
and the output form of ShowMessage() will display result path String as below,
1 2 3 |
D:\FirstFolder\SecondFolder\SubFolder |
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,
1 2 3 4 5 6 7 |
String path1 = L"D:\\FirstFolder\\"; String path2 = L"SecondFolder\\SubFolder"; String path = TPath::Combine( path1, path2); ShowMessage( path ); |
and the output form of ShowMessage() will display result path String as below,
1 2 3 |
D:\FirstFolder\SecondFolder\SubFolder |
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,
1 2 3 4 5 6 7 |
String path1 = L"D:\\FirstFolder\\"; String path2 = L"SecondFolder\\SubFolder"; String path = TPath::Combine( path1, path2); ShowMessage( path ); |
and the output form of ShowMessage() will display result path String as below,
1 2 3 |
D:\FirstFolder\SecondFolder\SubFolder |
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,
1 2 3 4 5 6 7 |
String path1 = L"D:\\FirstFolder"; String path2 = L"\\SecondFolder\\SubFolder"; String path = TPath::Combine( path1, path2); ShowMessage( path ); |
and the output form of ShowMessage() will display result path String as below,
1 2 3 |
D:\FirstFolder\SecondFolder\SubFolder |
as you see Combine Method() automatically combines two paths without adding “\” to between two paths.
Example 5
1 2 3 4 5 6 7 |
String path1 = L"D:\\FirstFolder"; String path2 = L"D:\\SecondFolder\\SubFolder"; String path = TPath::Combine( path1, path2); ShowMessage( path ); |
and this time the output form of ShowMessage() will display result path String as below,
1 2 3 |
D:\SecondFolder\SubFolder |
as you see Combine Method() returns the second one, because both has different paths.
Example 6
1 2 3 4 5 6 7 |
String path1 = L"D:\\FirstFolder"; String path2 = L"D:\\FirstFolder\\SubFolder"; String path = TPath::Combine( path1, path2); ShowMessage( path ); |
and in this example the output form of ShowMessage() will display result path String as below,
1 2 3 |
D:\FirstFolder\SubFolder |
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,
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#include <vcl.h> #include <IOutils.hpp> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { String path1, path2, path; // Example 1 path1 = L"D:\\FirstFolder"; path2 = L"SecondFolder\\SubFolder"; path = TPath::Combine( path1, path2); Memo1->Lines->Add("Example 1: "+path); // Example 2 path1 = L"D:\\FirstFolder\\"; path2 = L"SecondFolder\\SubFolder"; path = TPath::Combine( path1, path2); Memo1->Lines->Add( "Example 1: " + path ); // Example 3 path1 = L"D:\\FirstFolder"; path2 = L"\\SecondFolder\\SubFolder"; path = TPath::Combine( path1, path2); Memo1->Lines->Add( "Example 3: " + path ); // Example 4 path1 = L"D:\\FirstFolder"; path2 = L"D:\\SecondFolder\\SubFolder"; path = TPath::Combine( path1, path2); Memo1->Lines->Add( "Example 4: " + path ); // Example 5 path1 = L"D:\\FirstFolder"; path2 = L"D:\\FirstFolder\\SubFolder"; path = TPath::Combine( path1, path2); Memo1->Lines->Add( "Example 5: " + path ); } |
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.