C++ Builder has a lot of specific methods in its SysUtils library that are included in VCL and FMX libraries. Some of these are grouped as Path Manipulation Routines that allow a user to edit, extract, get and set drive name, directory name, file name, and file extensions. These methods are combined in the Vcl.FileCtrl, System.IOUtils, System.SysUtils libraries. These methods are easy to use and easy to get or set file path strings in that operating system. The functions 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.
In this post, you’ll learn how to determine whether two files are the same, what the SameFileName method in C++ is, and how to compare file names based on the current Windows locale. By learning how to check two file name strings if they are the same on the current locale. It will help you to easily build C++ applications using the C++ IDE.
Table of Contents
What is the C++ SameFileName method?
The SameFileName Method (System::SysUtils::SameFileName) is a SysUtils Method that compares file names based on the current locale. SameFileName compares two file names in the same way as AnsiCompareFileName. SameFileName returns True if the file names match, False otherwise. Like AnsiCompareFileName, SameFileName supports locales and MBCS strings; see the AnsiCompareFileName topic for more details.
The following table lists the parameters expected by this method:
Name | Meaning |
---|---|
Path | The verified file or directory name |
Note: that SameFileName method raises an exception if the given path contains invalid characters.
What is the syntax of the SameFileName method ?
Here is the syntax for the SameFileName Method,
1 2 3 |
bool __fastcall SameFileName(const System::UnicodeString S1, const System::UnicodeString S2); // overload |
Here is a simple example of using SameFileName Method
1 2 3 |
bool same = SameFileName( L"MYIMAGE.jpg", L"myimage.jpg" ); |
Is there a more complete example of how to use the C++ SameFileName method?
Here is the full C++ Builder VCL example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#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 filename = L"MYIMAGE.jpg"; if ( SameFileName( filename, L"myimage.jpg") ) Memo1->Lines->Add( L"Same file name"); else Memo1->Lines->Add( L"Not same file name"); } |
Note that can use this method with the OpenDialog (TOpenDialog) or SaveDialog (TSaveDialog) components. For example you can check if OpenDialog1->FileName is exactly same as the file name that you want.
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