In that case, that has nothing to do with C++.
If you have a new file type, and you want to associate a default program that will open when you click on a file, that is up to the operating system.
So let's say your program looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
if (argc <= 1)
{
std::cerr << "Usage: " << argv[0] << " <file>\n";
return 1;
}
std::cout << "You just opened a .test file: " << argv[1] << '\n';
std::cin.get();
return 0;
}
| |
Note the argv variable. argv[1] will be the path to your file when you double click on the .test file.
On Windows, for example, if your .test file is not already associated with any programs, then do the following:
- Right-click a .test file (e.g. my.test)
- Select Open
- On the pop-up, choose "Select a program from a list of installed programs"
- Browse...
- Navigate to your compiled .exe file, and choose it as the default program to run.
Now, when you double-click on a .test file, argv[1] will become the full path of the file you double-clicked on.
You can of course still have portable C++ code, but the instructions for setting up a default program to be associated with a different file extension will be different on, say, Linux.
Ubuntu:
https://help.ubuntu.com/stable/ubuntu-help/files-open.html.en
Linux Mint:
https://libre-software.net/change-the-default-application-linux-mint-ubuntu/
Gnome desktops:
https://help.gnome.org/admin/system-admin-guide/stable/mime-types-application-user.html.en