How to enable drag and drop of a file in * .exe and get it as a parameter?

What do I need to do so that my program uses a file that was dragged and dropped to its icon as a parameter?

My current main method is as follows:

 int main(int argc, char* argv[]) { if (argc != 2) { cout << "ERROR: Wrong amount of arguments!" << endl; cout << "\n" << "Programm closed...\n\n" << endl; exit(1); return 0; } Converter a(argv[1]); // ... cout << "\n" << "Programm finished...\n\n" << endl; // cin.ignore(); return 0; } 

What I really would like to do is select 10 (or so) files, drop them onto an EXE, and process them from my application.


EDIT:

The incomming parameter is used as the file name created in the cunstructor.

 Converter::Converter(char* file) { // string filename is a global variable filename = file; myfile.open(filename.c_str(), ios_base::in); } 

Text File Reading Method:

 string Converter::readTextFile() { char c; string txt = ""; if (myfile.is_open()) { while (!myfile.eof()) { myfile.get(c); txt += c; } } else { error("ERROR: can't open file:", filename.c_str()); } return txt; } 

EDIT2: Cross Out

Update:
I got to this point.

Actual main method:

 // File path as argument 

int main (int argc, char * argv []) {if (argc <2) {sob & A; <"ERROR: wrong number of arguments! Give at least one argument ... \ n" & L; <cps; cout <"\ n" <"The program is closed ... \ n \ n" <<nbsp; cin.ignore (); output (1); return 0; }

 vector<string> files; for (int g = 1; g < argc; g++) { string s = argv[g]; string filename = ""; int pos = s.find_last_of("\\", s.size()); if (pos != -1) { filename = s.substr(pos + 1); cout << "argv[1] " << argv[1] << endl; cout << "\n filename: " << filename << "\n pos: " << pos << endl; files.push_back(filename); } files.push_back(s); } for (unsigned int k = 0; k < files.size(); k++) { cout << "files.at( " << k << " ): " << files.at(k).c_str() << endl; Converter a(files.at(k).c_str()); a.getATCommandsFromCSV(); } cout << "\n" << "Programm finished...\n\n" << endl; cin.ignore(); return 0; } 

In fact, the console window appears, perhaps for 0.5 seconds, and closes again.
It does not stop at any of my cin.ignore(); Maybe he didn’t get there?

Can anyone help?

+6
c ++ windows parameters executable drag-and-drop
source share
3 answers

Your program should not do anything special other than handling command line arguments. When you drag a file into the application in Explorer, it no more than passes the file name as an argument to the program. Similarly for multiple files.

If all you expect is a list of file names, then just iterate over all the arguments, do whatever you want with them, and do it. This will work from zero to almost arbitrarily many arguments.

+14
source share

Perhaps you could write a test program as follows:

 int main(int argc, char* argv[]) { // argv[0] is not interesting, since it just your program path. for (int i = 1; i < argc, ++i) cout << "argv[" << i << "] is " << argv[i] << endl; return 0; } 

And let's see what happens after you throw different files on it.


EDIT: Just look at Joy's answer.

+2
source share

The answer to the main question

SEE ANSWER TO YOUR FOLLOWING PROBLEM TO SEE LOWER ANSWER

All files with drag and drop become available as argv[orderOfTheFile] ( orderOfTheFile - from 1-n) Once the windows create this order, now that is a real secret ...

In any case, let me say that I would create 26 simple text files (* .txt), from a.txt to z.txt on my desktop,
Now, if I drag and drop them onto my ArgsPrinter_c++.exe located directly on the C:\ drive, the result will be similar to this:

 argc = 27 argv[0] = C:\ArgsPrinter_c++.exe argv[1] = C:\Users\MyUserName\Desktop\c.txt argv[2] = C:\Users\MyUserName\Desktop\d.txt argv[3] = C:\Users\MyUserName\Desktop\e.txt argv[4] = C:\Users\MyUserName\Desktop\f.txt argv[5] = C:\Users\MyUserName\Desktop\g.txt argv[6] = C:\Users\MyUserName\Desktop\h.txt argv[7] = C:\Users\MyUserName\Desktop\i.txt argv[8] = C:\Users\MyUserName\Desktop\j.txt argv[9] = C:\Users\MyUserName\Desktop\k.txt argv[10] = C:\Users\MyUserName\Desktop\l.txt argv[11] = C:\Users\MyUserName\Desktop\m.txt argv[12] = C:\Users\MyUserName\Desktop\n.txt argv[13] = C:\Users\MyUserName\Desktop\o.txt argv[14] = C:\Users\MyUserName\Desktop\p.txt argv[15] = C:\Users\MyUserName\Desktop\q.txt argv[16] = C:\Users\MyUserName\Desktop\r.txt argv[17] = C:\Users\MyUserName\Desktop\s.txt argv[18] = C:\Users\MyUserName\Desktop\t.txt argv[19] = C:\Users\MyUserName\Desktop\u.txt argv[20] = C:\Users\MyUserName\Desktop\v.txt argv[21] = C:\Users\MyUserName\Desktop\w.txt argv[22] = C:\Users\MyUserName\Desktop\x.txt argv[23] = C:\Users\MyUserName\Desktop\y.txt argv[24] = C:\Users\MyUserName\Desktop\z.txt argv[25] = C:\Users\MyUserName\Desktop\a.txt argv[26] = C:\Users\MyUserName\Desktop\b.txt 

My ArgsPrinter_c++.exe source code:

 #include <iostream> using namespace std; int main(int argc, char* argv[]) { cout << "argc = " << argc << endl; for(int i = 0; i < argc; i++) cout << "argv[" << i << "] = " << argv[i] << endl; std::cin.ignore(); return 0; } 

Your last problem

I created a simple program that only creates the sceleton of your class so that it can be used, and the program itself ran JUST FINE => if your program exits too early, the problem will be in its class ...

Tested source code:

 #include <iostream> #include <vector> using namespace std; class Converter{ public: Converter(const char* f){ cout << f << endl; } void getATCommandsFromCSV(){ cout << "called getATCommandsFromCSV" << endl; } }; int main(int argc, char* argv[]) { vector<string> files; for (int g = 1; g < argc; g++) { string s = argv[g]; string filename = ""; int pos = s.find_last_of("\\", s.size()); if (pos != -1) { filename = s.substr(pos + 1); cout << "argv[1] " << argv[1] << endl; cout << "\n filename: " << filename << "\n pos: " << pos << endl; files.push_back(filename); } files.push_back(s); } for (unsigned int k = 0; k < files.size(); k++) { cout << "files.at( " << k << " ): " << files.at(k).c_str() << endl; Converter a(files.at(k).c_str()); a.getATCommandsFromCSV(); } cout << "\n" << "Programm finished...\n\n" << endl; cin.ignore(); return 0; } 
+1
source share

All Articles