How to associate a file type with my program

Hello. How can I get a certain file type to open with my program such as
a .test file?

What header do I include? What do I write?? Please keep it simple.

Thanks in advance.
File extensions do not encode information. They're just to help humans organize their file systems, and let operating systems associate default programs with certain file extensions.

Here is how you open a file:
1
2
3
4
5
6
#include <fstream>

int main()
{
    std::ifstream fin("thing.test");
}


If it's a binary (non-plaintext) file, you might want to open it in binary mode,
1
2
3
4
5
6
#include <fstream>

int main()
{
    std::ifstream fin("thing.test", std::ios::binary);
}


What matters for a file is how the information it contains is structured. For example, a PNG file format has particular rules for how it's encoded/decoded to save/display an image. The fact that the extension is ".png" is just to help us, like I mentioned.

So, what you actually need to know is how this ".test" file you mention is actually structured, to be able to extract the right information out of the file.
Last edited on
But I mean to make it open like a batch file, but with my program and make it do something
Batch files are windows only so they won't work.
You need to give more information.
What information does this .test file contain?
You're saying it's like a batch file?

Again, the extension doesn't matter. It could be .bat, .test, .asdfhjk. What matters is the contents of the file. It's just that normal conventions dictate that ".bat" usually means "Windows batch file", but it doesn't have to mean that.

So the file contains commands in plaintext? If so, what should your program do with these commands?

__________________________

Here is an example of opening a file and printing each line in it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::ifstream fin("thing.test");

    std::string line;
    while (std::getline(fin, line))
    {
        std::cout << line << '\n';
    }
}
Last edited on
I just want a file type to have easy access through being clicked on to open with my program.
Last edited on
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
Last edited on
Well then how does Dev-cpp make the .cpp files be associated with itself???

I want to know how THAT works...
Same directions as above, except now that .cpp already has a default program associated with it, you need to do:
- Right-click file
- Select "Open With..."
- Select "Choose default program..."
- Browse for your Dev C++ .exe (I don't know exactly what that is, but find wherever you have installed Dev-cpp)
- Select the .exe as the default program
Last edited on
Dev-cpp is an c++ IDE. outdated, I've been told.

You are saying this is how Dev-cpp made the Dev-cpp .cpp or .hpp file icon show up
No, I'm saying that's how the Dev-cpp program associates itself with a file extension, to be invoked when you double-click on .cpp or .hpp files. Through operating system configuration.

File icons are a different matter than default programs...
The file icon is typically baked into the program when it's made. I guess you can also change it later with the right tools.
See: https://stackoverflow.com/questions/2393863/set-an-exe-icon-for-my-program
Or for GCC (MinGW): https://stackoverflow.com/questions/708238/how-do-i-add-an-icon-to-a-mingw-gcc-compiled-executable
Last edited on
in windows you can associate a type to a program in the registry.
I do not know what header you need but visual studio has a header and utility / functions that can modify the registry, or you can also do it by making a .reg file and installing it.
I believe installer-making tools can also set up registry entries. This is the most usual way to do it, in my 'as a user' experiences.
Last edited on
Topic archived. No new replies allowed.