c++ - Windows drag and drop problem with console app -
I have a program that creates a file and writes it using offsm. I need the program to be able to parse the command line parameters later. But for some reason, this does not create a file when I drag and drop a file on the compile executable, even if the program does not include any command-line parameters. If executable is run normally, then it works, so I'm completely left confused. Here's the source:
#include & lt; Iostream & gt; # Include & lt; Fstream & gt; using namespace std; Int main () {ofstream outfile; Outfile.open ("test.txt"); If (outfile.is_open ()) {outfile & lt; & Lt; "testing"; Outfile.close (); } And cout & lt; & Lt; "unable to open file"; Return 0; }
Do anyone have any ideas? I appreciate any help.
You are not using command line arguments at all. To look like this, repeat your main method:
int main (int argc, char ** argv) {if (argc! = 2) {cout & lt; & Lt; "Usage: blah.exe file" & lt; & Lt; Endl; Return 1; } Offline outfile; Outfile.open (argv [1]); If (outfile.is_open ()) {outfile & lt; & Lt; "testing"; Outfile.close (); } And cout & lt; & Lt; "unable to open file"; Return 0; }
Be careful what you leave, your code rewrites the file content.
Comments
Post a Comment