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 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 on a given path. We can include and exclude path delimiters on Windows.
In this post, you’ll learn how to add a path delimiter to a path string. What is the C++ ExcludeTrailingPathDelimiter, What is the IncludeTrailingPathDelimiter method, What is the difference between ExcludeTrailingBackslash, and ExcludeTrailingPathDelimiter methods, and What is the difference between IncludeTrailing PathDelimiter and IncludeTrailing PathDelimiter methods? Learning how to use, include, and exclude path delimiters on Windows will allow you to easily build C++ applications using the C++ IDE.
Table of Contents
Where can I see more examples of the Path Manipulation routines?
Go to this tag search: https://learncplusplus.org/tag/path-manipulation-routines/
What is the C++ ExcludeTrailingPathDelimiter method ?
The ExcludeTrailingPathDelimiter Method (System.SysUtils.ExcludeTrailingPathDelimiter) is a SysUtils Method that returns a path name without a trailing delimiter. In another terms this method returns UnicodeString, with the trailing path delimiter (PathDelim, ‘\’ on Windows, ‘/’ otherwise) removed. If the last character in S
is not a delimiter, then S
is returned unchanged. Note that ExcludeTrailingPathDelimiter works with multibyte character sets (MBCS).
Platform | Sample Input (S) | Output |
---|---|---|
Windows | C:\Your\Input\Path C:\Your\Input\Path\ | C:\Your\Input\Path C:\Your\Input\Path |
OS X iOS Android Linux | /Your/Input/Path /Your/Input/Path/ | /Your/Input/Path /Your/Input/Path |
What is the syntax of ExcludeTrailingPathDelimiter method ?
Here is the ExcludeTrailingPathDelimiter Method:
1 2 3 |
UnicodeString __fastcall ExcludeTrailingPathDelimiter(const System::UnicodeString S); // overload |
What is the C++ IncludeTrailingPathDelimiter method ?
IncludeTrailingPathDelimiter Method (System.SysUtils.IncludeTrailingPathDelimiter) is a SysUtils Method that ensures that path name ends with a delimiter. In other terms this method ensures that a path name ends with a trailing path delimiter (PathDelim, ‘\’ on Windows, ‘/’ otherwise). If S
already ends with a trailing delimiter character, it is returned unchanged; otherwise, S
is returned with an appended delimiter character. Note that IncludeTrailingPathDelimiter works with multibyte character sets (MBCS).
Platform | Sample Input (S) | Output |
---|---|---|
Windows | C:\Your\Input\Path C:\Your\Input\Path\ | C:\Your\Input\Path\ C:\Your\Input\Path\ |
OS X iOS Android Linux | /Your/Input/Path /Your/Input/Path/ | /Your/Input/Path/ /Your/Input/Path/ |
What is the syntax of IncludeTrailingPathDelimiter method?
Here is the IncludeTrailingPathDelimiter Method:
1 2 3 |
UnicodeString __fastcall IncludeTrailingPathDelimiter(const System::UnicodeString S); // overload |
What are the simple examples of using the C++ ExcludeTrailingPathDelimiter and IncludeTrailingPathDelimiter methods?
ExcludeTrailingPathDelimiter and IncludeTrailingPathDelimiter methods return UnicodeString, we can use them as given below,
1 2 3 4 5 6 7 |
String path1 = IncludeTrailingPathDelimiter( L"D:\\MyFolder" ); ShowMessage(path1); String path2 = ExcludeTrailingPathDelimiter( L"D:\\MyFolder\\" ); ShowMessage(path2); |
and the outputs will be as follows respectively,
1 2 3 |
D:\MyFolder\ |
and,
1 2 3 |
D:\MyFolder |
Here is a full example of the C++ ExcludeTrailingBackslash and IncludeTrailingBackslash methods
Here is the example how we check if a directory exists or not,
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 |
#include <vcl.h> #pragma hdrstop #include "Include_Exclude_Path_Delimiters_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { String path1, path2, path; path1 = L"D:\\MyFolder"; path2 = L"D:\\MyFolder\\"; Memo1->Lines->Add( "Path: " + path1 ); path = IncludeTrailingPathDelimiter( path1 ); Memo1->Lines->Add( "IncludeTrailingBackslash: " + path ); Memo1->Lines->Add( "Path: " + path2 ) ; path = ExcludeTrailingPathDelimiter( path2 ); Memo1->Lines->Add( "IncludeTrailingBackslash: " + path ); Memo1->Lines->Add( "Path: " + path1 ); path = IncludeTrailingBackslash( path1 ); Memo1->Lines->Add( "IncludeTrailingDelimiter: " + path ); Memo1->Lines->Add( "Path: " + path2 ) ; path = ExcludeTrailingBackslash( path2 ); Memo1->Lines->Add( "IncludeTrailingDelimiter: " + 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.