|
Title: File Open Problem in C/C++ Post by: theweirdn8 on February 15, 2013, 07:20:32 AM Okay, So I will like for my program to Open and process a file the user specified via the O.S Menu choice. I Google-Searched like crazy and I have not been able to find the solution. For those a bit confused as to what I'm doing. Here is screenshots that should explain: (http://i46.tinypic.com/29bzdxv.png) (http://i50.tinypic.com/2n1xf8w.png) Thank you for your help and time. Title: Re: File Open Problem in C/C++ Post by: makerimages on February 15, 2013, 07:24:30 AM Why would you want to do that? A simple but annoying way should be to have the user type the full file path using cin or something. Then you could open it with the regular file handling code.
Title: Re: File Open Problem in C/C++ Post by: Liosan on February 15, 2013, 08:46:34 AM So, uh, what is the "O.S Menu choice" ?
Are you looking for file-type association? You can do that manually be pointing Windows to the exec file. The simplest way to pass an arbitrary file path to your program in Windows is by drag-and-drop. Just drop a file on your program (or a shortcut to it) and it will be passed as command-line argument 1. If you get file-type association to work, the you program will also receive the file path as command-line argument 1. Besides, I don't think this thread has anything to do with C/C++, since this convention is independent of the programming language used (as applied across many OSes, not only Windows). Liosan Title: Re: File Open Problem in C/C++ Post by: Evan Balster on February 15, 2013, 10:59:05 AM If you want ".myawesomefiletype" files to be associated with your programm, use an installer to make that association. NSIS (http://nsis.sourceforge.net/Main_Page) does an excellent job of this.
Title: Re: File Open Problem in C/C++ Post by: theweirdn8 on February 15, 2013, 12:31:50 PM I'm not sure if I communicated the issue wrong, but what I'm trying to achieve is basically how MicroSoft Word opens up .doc files and processes it, that is what I'm trying to do in my programs. If I simply drag it into the program, how would the program know the file is there and how can I code to process it? I'm looking to learn how to do it the same way we can open up files by simply clicking on it after we declared that extension to work with xyz programs. Title: Re: File Open Problem in C/C++ Post by: Liosan on February 15, 2013, 01:01:09 PM I'm pretty sure we communicated this already:
If I simply drag it into the program, how would the program know the file is there and how can I code to process it? Just drop a file on your program (or a shortcut to it) and it will be passed as command-line argument 1 --I'm looking to learn how to do it the same way we can open up files by simply clicking on it after we declared that extension to work with xyz programs. Are you looking for file-type association? You can do that manually be pointing Windows to the exec file. If you want ".myawesomefiletype" files to be associated with your programm, use an installer to make that association. NSIS (http://nsis.sourceforge.net/Main_Page) does an excellent job of this. If that's not the answers you're looking for... give us better questions :shrug2: Liosan Title: Re: File Open Problem in C/C++ Post by: impulse9 on February 16, 2013, 01:56:25 AM Basically all Windows programs operate the same way. When you drag a file in Windows Explorer to your app, it passes it as arg[1]. So then you just start the program in the usual way and load the file provided.
Title: Re: File Open Problem in C/C++ Post by: kamac on February 16, 2013, 02:21:16 AM Example:
Code: #include <Windows.h> #include <iostream> int main(int argc, char* argv[]) { if(argc >= 2) { std::cout << argv[1] << "\n"; system("pause"); } else { std::cout << "No file choosen. Aborting.\n"; system("pause"); } return 0; } Title: Re: File Open Problem in C/C++ Post by: Rusk on February 16, 2013, 07:13:37 AM I suppose adding something like this to the registry would let you open it with a program of your choice.
http://msdn.microsoft.com/en-us/library/cc144158%28VS.85%29.aspx Title: Re: File Open Problem in C/C++ Post by: theweirdn8 on February 16, 2013, 02:23:33 PM Hey guys thank you. The args part was what I was looking for. This is pretty interesting, since for some reason, loading in files took in the directory of the file being read. That shouldn't be too hard to solve. Thank you guys for all of the lovely support. |