How can I get file extension from a path string in a C++app on Windows PC? What is the GetExtension Method? What is the syntax of the GetExtension method? Is there an example of how to use the GetExtension Method?
Table of Contents
The C++ Builder IO libraries
C++ Builder has a lot of specific methods in its SysUtils and IOUtils 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, file extensions, and 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 the drive, files, or directories to see if they exist or not in that given path. And we can get file extension from a given path string.
Let’s see how we can use GetExtension Method to get extension of a file from a path string file name on Windows.
What is the GetExtension method?
GetExtension (System::IOUtils::TPath::GetExtension) is a Path Manipulation Routine that extracts the extension part from a path string file name. GetExtension extracts the extension part of the given file name. The resulting string also includes the dot that separates the extension from the path. If FileName has no extension, the resulting string is empty. The following table lists the parameters expected by this method.
Name | Meaning |
---|---|
FileName | The file name from which the extension is extracted. |
Note: GetExtension raises an exception if the given file name contains invalid characters.
What is the syntax of the GetExtension method?
Here is the Syntax of GetExtension() Method:
1 2 3 |
UnicodeString __fastcall GetExtension(const System::UnicodeString FileName); |
A simple example of using the GetExtension method in a C++ app
The GetExtension Method returns a UnicodeString, We can get the file extension from a path string as in example below,
Example 1:
Let’s see return from a full path string of a file.
1 2 3 4 |
String ext = TPath::GetExtension( L"D:\\MainFolder\\SubFolder\MyFile.jpg"); ShowMessage(ext); |
and the output form of ShowMessage() will display this result extension String,
1 2 3 |
.jpg |
Here is a more complete C++ app example of using the GetExtension method
C++ Builder has OpenDialog, SaveDialog, and other components that allow you browse files by a File Browser to save or to load a file. These dialogs also have a Filter Property, so you can filter listed files in given extensions. Nevertheless, some users may force filename extensions in their mind. Thus you can use GetExtension() method to check FileName property of these Dialogs.
Here is the full C++ Builder VCL example with the OpenDialog (TOpenDialog) component that checks if selected filename is “.jpg” or “.png” in any case format. See example 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 |
#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) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { if(OpenDialog1->Execute()) { String ext = TPath::GetExtension( OpenDialog1->FileName); if( ext.LowerCase() == L".jpg" || ext.LowerCase() == L".png" ) { ShowMessage("Choosen File is JPG or PNG"); } } } |
This example can be used to limit users in different filetypes. For example, it can be used to upload images to a database, some files take a lot of size (for example .bmp), so you can limit users with .png, .jpg, .gif files. This will help you a lot to speed up your other database operations on these images.
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition