Hi, I was wondering what it is called when a file starts a program and uses the file's data to do certain things. Sorry if that doesn't make sense but I don't know what it would be called. Some examples I know so far are:
.txt files open notepad and displays whats in the text
any picture file opens Window's Photo Gallery and displays the picture
I'm not too sure if it matters or not seeing that it may be an operating system type thing. But the language I code in is C++.
I don't want you guys to tell me how to do it obviously I just would like to know what it's called so I can search it and learn on my own. :p
Thanks in advanced and sorry if this sounds confusing in any way I just don't know any other way to describe it.
As far as I know, the fact that a certain program is opened automatically for a certain file, is a OS configuration thing (so much so, that you can tell windows to open .txt files with notepad++, for example).
As for the program using the file, most programs probably take an argument when they're called which is the path to the file, and then do something like fopen() to access its contents, and go from there.
Hope this helps
PS: I never coded anything that required file access for windows, so I don't know if the argument system or fopen() works at all. But for linux, that's what I'd do
That's just associating a program with a file-type.
You don't want much information on how to do it, but here's a hint: registry.
Command line arguments are not difficult to learn, ever see:
[code]
int main( int argc, const char* argv[] )
[/code]
in a c++ tutorial? That's your command line arguments.
The int is the number of args in the array, the character array is the actual arguments.
As for making windows use your program to open the file, one way is to right click a file of the right type -> Open With -> Browse -> then find your exe and press ok. Windows should register your exe to opening that specific format.
As for getting it done automatically (ie after installing or running it once) you will need to look into regediting.
Thanks guys for all the help. And acutally I have seen that in the main function many times. Unfortunately I always did :
[code]
int main()
[/code]
But thanks I'll look all the information you gave me up.
[editline]01:26PM[/editline]
Sorry but just in case anyone did want an example on how to do it. I made a test program and it worked. But almost all my credit goes to these people that helped. I just looked it up and implied it in my code.
[code]
#include <iostream>
#include <fstream>
#include <string>
int main( int argc, const char* argv[] )
{
if(argc > 1) //The first arg goes up when the program is ran, there would be another one if a file ran the program
{
std::ifstream File;
File.open(argv[1]); //argv[1] is the file's directory
std::string Data = "";
while(File.eof() == false)//untill the file is at the end run this loop
{
File >> Data; //Set the string to the content of the File
std::cout<<"\n"<<Data;//Display the string on console
}
while(1)//Stop the program from automaticly closing when clicking the file
{
}
}
}
[/code]
And as for the file itself, I did have to set the program it ran. I'll look up doing it automatically soon though. Thanks again for the information.
I have some code (C++) that I did a few years ago that registers a file extension to a program. You said you wanted to do it yourself for learning, but just tell me if you want it.
[QUOTE=xAustechx;19954104][code] while(1)//Stop the program from automaticly closing when clicking the file
{
}
[/code]
[/QUOTE]
Don't do this. What I use is [code]system("pause");[/code]
It's that "Press any key to continue..." line.
Don't troll, agent.
[QUOTE=efeX;19972569]Don't troll, agent.[/QUOTE]
I don't think he is trolling D:
[QUOTE=Agent766;19967991]Don't do this. What I use is [code]system("pause");[/code]
It's that "Press any key to continue..." line.[/QUOTE]
Don't do that either, though it's better than while(1). Better yet would be to put an input statement of some sort at the end of the program, for example:
[cpp]
std::cout << "Press Enter to continue..." << std::flush;
std::cin.get();
[/cpp]
This way you're using your program's own ability to await input, which is much more straightforward than asking the operating system to create a new process, run cmd.exe in it, and feed a command to it, just to ask the user for a keystroke.
Sorry, you need to Log In to post a reply to this thread.