Command Line Parameters Prevents ofstream

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>
using namespace 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;
}
Last edited on
Are you sure it's not creating the file in a place yo do not expect it to? You might want to pass a full path to outfile.open() and see if that helps.
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.
You got it exactly backwards.
Thought so. After all, \n is a newline.
No, I mean "/file" is a file in the root, and "file" (or "./file") is a file in the working directory.
Wow, really?
Hmm, I suppose that still makes sense.
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.
Last edited on
When a file is dragged to a program, the working directory should be irrelevant, since the program is passed the absolute path to the file.
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?
yoonkwun wrote:
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.

http://www.boost.org/doc/libs/1_41_0/libs/filesystem/doc/index.htm
Topic archived. No new replies allowed.