Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1066654 Posts in 43538 Topics- by 35572 Members - Latest Member: Joe Lili

November 26, 2014, 12:46:39 AM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)File Open Problem in C/C++
Pages: [1]
Print
Author Topic: File Open Problem in C/C++  (Read 659 times)
theweirdn8
Level 2
**

theweirdn8@live.com legendarynate nateisoraw
View Profile WWW Email
« 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:



Thank you for your help and time.
Logged


-Check out my nice little Farming Game that is more than just raising farm animals and crops..
makerimages
Level 2
**


Makerimages Studios


View Profile WWW Email
« Reply #1 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.
Logged

Makerimages-Its in the pixel
Liosan
Level 2
**


View Profile
« Reply #2 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
« Last Edit: February 15, 2013, 09:13:52 AM by Liosan » Logged

Evan Balster
Level 10
*****


I live in this head.


View Profile WWW Email
« Reply #3 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 does an excellent job of this.
Logged

Creativity births expression.  Curiosity births exploration.
Our work is as soil to these seeds; our art is what grows from them...


Wreath, SoundSelf, Infinite Blank, Cave Story+, <plaid/audio>
theweirdn8
Level 2
**

theweirdn8@live.com legendarynate nateisoraw
View Profile WWW Email
« Reply #4 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.
Logged


-Check out my nice little Farming Game that is more than just raising farm animals and crops..
Liosan
Level 2
**


View Profile
« Reply #5 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 does an excellent job of this.

If that's not the answers you're looking for... give us better questions Shrug

Liosan
Logged

impulse9
Level 5
*****



View Profile WWW
« Reply #6 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.
Logged

kamac
Level 10
*****


Notorious posts editor


View Profile Email
« Reply #7 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;
}
Logged

Nothing to do here
Rusk
Level 1
*


View Profile
« Reply #8 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
Logged
theweirdn8
Level 2
**

theweirdn8@live.com legendarynate nateisoraw
View Profile WWW Email
« Reply #9 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.
Logged


-Check out my nice little Farming Game that is more than just raising farm animals and crops..
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic