Bea*_*sly 6 c++ windows parameters executable drag-and-drop
我需要做什么才能使我的程序使用已拖放到其图标上的文件作为参数?
我目前的main方法如下:
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;
}
Run Code Online (Sandbox Code Playgroud)
我真正希望能够做的是选择10个(或者更多)文件,将它们放到EXE上,然后在我的应用程序中处理它们.
编辑:
incomming参数用作文件名,在cunstructor中构造.
Converter::Converter(char* file) {
// string filename is a global variable
filename = file;
myfile.open(filename.c_str(), ios_base::in);
}
Run Code Online (Sandbox Code Playgroud)
读取文本文件的方法:
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;
}
Run Code Online (Sandbox Code Playgroud)
EDIT2: 已删除
更新:
我再次到了这一点.
实际Main方法:
// File path as argument
Run Code Online (Sandbox Code Playgroud)
int main(int argc,char*argv []){if(argc <2){cout <<"ERROR:参数数量错误!至少给出一个参数...... \n"<< endl; cout <<"\n"<<"程序关闭...... \n \n"<< endl; cin.ignore(); 出口(1); 返回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;
}
Run Code Online (Sandbox Code Playgroud)
实际上,控制台窗口可能会持续0.5秒并再次关闭.
它不会停留在我的任何一个cin.ignore();也许它没有到达那里?
有人可以帮忙吗?
Joe*_*oey 15
除了处理命令行参数之外,您的程序不需要执行任何特殊操作.当您将文件拖放到资源管理器中的应用程序时,它只会将文件名作为参数传递给程序.同样适用于多个文件.
如果你所期望的只是一个文件名列表,那么只需遍历所有参数,随心所欲地做任何事情并完成.这将适用于零到几乎任意多的参数.