Windows drag and drop issue with console application

I have a program that creates a file and writes it using streamstream. I need a program to subsequently analyze command line options. But for some reason, it does not create the file when I drag and drop the file onto the compiled executable, even if the program does not contain command line parameters at all. If the executable runs normally, it works. Therefore, I am completely confused. Here is the source:

#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; } 

Does anyone have any ideas? I appreciate any help.

+4
source share
4 answers

You do not use command line arguments at all. Recode your main () method to look like this:

 int main(int argc, char** argv) { if (argc != 2) { cout << "Usage: blah.exe file" << endl; return 1; } ofstream outfile; outfile.open(argv[1]); if(outfile.is_open()) { outfile << "Test"; outfile.close(); } else cout << "Unable to open file"; return 0; } 

Be careful what you drop, your code overwrites the contents of the file.

+1
source

The following code does what the OP wants:

 #include <iostream> #include <fstream> using namespace std; int main ( int argc, char ** argv ) { cout << argv[1] << endl; ofstream outfile; outfile.open("testzzzzzzz.txt"); if(outfile.is_open()) { outfile << "Testzzzzz"; outfile.close(); cout << "wrote file"<< endl; } else cout << "Unable to open file"; string s; getline( cin, s ); return 0; } 

It allows you to drag and drop, but does not use the name of the dropped file in the opened file. When you drop a file into it, you get a message

"wrote file"

Unfortunately, at the moment I have no idea where he wrote the file - not in the current directory, definitely. Just do a search ...

Edit:. He creates it in the Documents and Settings directory. Therefore, to put it in the current directory, you probably need to explicitly specify it as "./", but I did not check this - I leave it as an exercise for the reader :-)

+1
source

you did not specify a path for "test.txt", so it will try to create this file in the current working directory of the executable file. This will be different if exe is called by deleting the file on it than during normal program operation.

Try giving "test.txt" the full path and see if this works.

edit: To write the output file to a path containing exe, you should use

GetModuleFileName(NULL, ...) to the full exe path, then PathRemoveFileSpec to remove the exe name, leaving only the exe path, then PathCombine add the test.txt file to the exe path

0
source

Since you did not specify a path, the test.txt file will be saved to the default path. Just call the command line (i.e., run cmd.exe) and the default path will be shown on the command line. The file must be in this directory.

You can change the default path by editing the HOMEDRIVE and HOMEPATH environment variables.

In addition, you should note other answers. You must use argc / argv to specify the output file.

0
source

Source: https://habr.com/ru/post/1299274/


All Articles