8

What do I have to do to make my program use a file that has been dragged and dropped onto its icon as a parameter?

My current main method looks like this:

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'd really like to be able to do is select 10 (or so) files, drop them onto the EXE, and process them from within my application.


EDIT:

The incomming parameter is used as filename, constructed in the cunstructor.

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

The method where the textfile gets read:

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: deleted

Update:
I got again to this point.

Actual Main method:

// File path as argument

int main(int argc, char* argv[]) { if (argc < 2) { cout << "ERROR: Wrong amount of arguments! Give at least one argument ...\n" << endl; cout << "\n" << "Programm closed...\n\n" << endl; cin.ignore(); exit(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;
}

Actually the console window apears for maybe 0.5 sec and closes again.
It doen't stop on any of my cin.ignore(); Maybe it doesn't get there?

Can anyone help?

2
  • See my answer below and feel free to ask any questions related...
    – jave.web
    Mar 4, 2015 at 1:03
  • I had a problem that was similar to this. I added std::cout << "pause"; before the loop that iterates through arguments, and somehow it started working.
    – feos
    Apr 22, 2018 at 7:54

3 Answers 3

19

Your program does not need to do anything special apart from handling command-line arguments. When you drag-drop a file onto an application in Explorer it does nothing more than to pass the file name as argument to the program. Likewise for multiple files.

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

10
  • I tried with just one file and it did nothing... The program (console window) appeared for < 1sec. and nothing happened. In contrast giving the parameter manually through the console works fine. Thats why I'm asking...
    – Beasly
    Jan 11, 2011 at 16:48
  • @Beasly: You do realize that the console window disappears as soon as your program finishes running? Put a pause in there or wait for a keypress so you can see the output.
    – Joey
    Jan 11, 2011 at 17:00
  • Yes I do ;) The thing is, I manipulate a text file and save it with another name. Example: input: test.txt output: text_c.txt - actually this file isn't made. As I said manually it works...
    – Beasly
    Jan 11, 2011 at 17:23
  • @Beasly: Does your code have problems anywhere with certain characters, such as spaces? Explorer will almost certainly pass the complete, absolute path to the file instead of just the file name. Also, you might be creating the new file in the program's working directory, which is not necessarily next to the original file.
    – Joey
    Jan 11, 2011 at 17:27
  • Do you mean spaces in the file path? So do you think I should try to cut the String into filename only and try this?
    – Beasly
    Jan 11, 2011 at 18:13
3

Maybe you could write a test program like this:

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

    return 0;
}

And see what happens after you throw different files at it.


EDIT: Just look at Joey's answer.

1
  • +1 anyway. From the comments on my answer we're exactly at that point now again: Writing a test program that outputs the arguments ;-)
    – Joey
    Jan 11, 2011 at 18:28
1

Answer to the main question

TO SEE THE ANSWER TO YOUR LAST PROBLEM SEE BOTTOM OF THIS ANSWER

All drag&dropped files are get-able as argv[orderOfTheFile] (orderOfTheFile is from 1-n),
however how does windows create that order, now that is a real mystery...

Anyway let's say I would create 26 plain text files ( *.txt ), from a.txt to z.txt on my Desktop,
now if I would drag&dropped them on my ArgsPrinter_c++.exe located directly on C:\ drive,
an output would 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 have created a simple program that creates only a sceleton of your class so it can be used, and the program's main itself ran JUST FINE => if your program exits too soon, the problem will be in your 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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.