In this post, you’ll learn how to get a full path from a path string in C++ Software. What are the GetFullPath Method, its syntax, and examples of how to use it?
Table of Contents
How to understand paths on Windows?
The full path is also known as the absolute path. It begins with a drive letter followed by a colon, such as D: For example, this is a full path file name.
1 2 3 |
D:\Projects\MyProject\Win32\Debug\MyApp.exe |
The current path is the path that is normally file path of your application that you are running. We can change the current path on runtime, so every new folder or file operation may occur in that path.
1 2 3 |
D:\Projects\MyProject\Win32\Debug\ |
The relative path is the path that is ‘relative’ to a current directory – by that we mean at the same level or a deeper level, but without having the full path expressed. We can use a dot (.) and a double-dot (..) to translate it as the current directory and the parent directory. Here single dot represents the current directory itself and double dots are used for parent directory.
Let’s assume our app is stored as “D:\Projects\MyProject\Win32\Debug\MyApp.exe
“. The relative path of the folder:
1 2 3 |
D:\Projects\MyProject\Win32\Debug\Images\ |
is equal to:
1 2 3 |
Images |
Are there any other C++ Builder path methods?
C++ Builder has a lot of specific methods in its SysUtils and IOUtils library that are included in VCL and FMX libraries. Some of these are grouped as a Path Manipulation Routines that allow the user to edit, extract, get and set drive name, directory name, file name, file extensions and so on. These methods are combined in Vcl.FileCtrl, System.IOUtils, System.SysUtils libraries. These are all methods which 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 if they exist or not in that given path. And we can get file name from a given path string.
Let’s see how we can use GetFullPath Method to get full path from a a path string file name on Windows.
What is the GetFullPath method?
GetFullPath (System::IOUtils::TPath:: GetFullPath) is a Path Manipulation Routine that returns the absolute path for a given path. The full path, also known as the absolute path, begins with a drive letter followed by a colon, such as D: For example this is a full path file name: D:\myfolder\anotherfolder\myapp.exe
GetFullPath returns the full, absolute path for a given relative path. If the given path is absolute, GetFullPath simply returns it; otherwise, GetFullPath uses the current working directory as a root for the given path. The following table lists the parameters expected by this method:
Name | Meaning |
---|---|
Path | The relative path |
Note: GetFullPath raises an exception if the given path contains invalid characters.
What is the Syntax of the GetFullPath Method ?
Here is the Syntax of GetFullPath() method,
1 2 3 |
UnicodeString __fastcall GetFullPath(const System::UnicodeString Path); |
Here the FileName is the file name from which the name and extension parts should be extracted.
Note: GetFileNameWithoutExtension raises an exception if the given file name contains invalid characters.
Here is a simple example of how to use the GetFullPath Method
GetFileNameWithoutExtension Method returns a UnicodeString. With it we can get the file name without an extension from a path string as in example below,
Example:
Assume that you have an application in this path;
1 2 3 |
D:\Projects\MyProject\Win32\Debug\MyApp.exe |
If we run this MyApp.exe and if we use GetFullPath Method as in example below,
1 2 3 4 |
String filename = TPath::GetFileNameWithoutExtension( L"..\Test" ); ShowMessage( filename ); |
the output of ShowMessage() will display this result path string as below,
1 2 3 |
D:\Projects\MyProject\Win32\Test |
Is there a full example of using the GetFullPath method?
Here is the full example to GetFullPath Method,
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 |
#include <vcl.h> #include <IOUtils.hpp> #pragma hdrstop #include "Get_Methods_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { path = GetCurrentDir(); Memo1->Lines->Add("GetCurrentDirectory: " + path); // ShowMessage(path); path = TPath::GetFullPath( L"." ); Memo1->Lines->Add("GetFullPath: " + path); // ShowMessage(path); path = TPath::GetFullPath( L"..\\Test"); Memo1->Lines->Add("GetFullPath ..\\Test: " + path); // ShowMessage(path); } |
If we run this example in this Debug folder;
1 |
D:\#PROJECTS\#LearnCPlusPlus.org\Path Manipulation Routines\Get Methods\Win32\Debug |
The output will be as follows:
1 2 3 4 5 6 7 |
GetCurrentDirectory: D:\#PROJECTS\#LearnCPlusPlus.org\Path Manipulation Routines\Get Methods\Win32\Debug GetFullPath: D:\#PROJECTS\#LearnCPlusPlus.org\Path Manipulation Routines\Get Methods\Win32\Debug GetFullPath ..\Test: D:\#PROJECTS\#LearnCPlusPlus.org\Path Manipulation Routines\Get Methods\Win32\Test |
as you see if we ask for L”.” it returns same full path of current directory that we run the application. You can copy in other folders and run your executable to see changes.
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.