When I drag and drop a file onto this compiled program, it will run but it will not create a file like it's supposed to. But if I run it by itself it will, even though the program doesn't involve the command line parameters at all. What's going on?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <fstream>
usingnamespace std;
int main ()
{
ofstream outfile;
outfile.open("test.txt");
if(outfile.is_open())
{
outfile << "Test";
outfile.close();
}
else cout << "Unable to open file";
return 0;
}
If you have open("name") it creates to the drive root. If you have open("\name") (my slash may be the wrong way around) it will create to the directory of the prog.
Thanks all, I've found out that with "file", the working directory is set to the directory of the executable only if the program is run normally. With a file dragged onto it, the working directory is in the default path (for me it was C:\Documents and Settings\HP_Owner).
Does anybody know how I would set the working directory to the directory the executable is in? Basically what I want is, a random file (doesn't have to be in the executable path) is dragged onto the executable. Then the program will create another file, totally irrelevant to the dragged file, into the executable directory, just for testing purposes. I want it to work no matter what directory the executable is in.
Also I forgot to mention I'm working with Windows XP SP3 and I'm using MinGW as my compiler.
Okay. Can anyone give me info on how to create a file in the path the executable is in? I know the full executable path is passed in argv[0] so would I have to get rid of the executable name in argv[0] for it? If so how would I do that?
Okay. Can anyone give me info on how to create a file in the path the executable is in? I know the full executable path is passed in argv[0] so would I have to get rid of the executable name in argv[0] for it? If so how would I do that?
The Boost Filesystem library has functions for this.