我需要做些什么才能让我的程序使用已拖放到其图标上的文件作为参数?
我当前的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;
}
我真正希望能够做的就是选择10个(或者更多)文件,将它们放到EXE上,然后在我的应用程序中处理它们。
修改
incomming参数用作文件名,在构造函数中构造。
Converter::Converter(char* file) {
// string filename is a global variable
filename = file;
myfile.open(filename.c_str(), ios_base::in);
}
读取文本文件的方法:
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: 删除
更新
我再次到了这一步。
实际Main
方法:
// File path as argument
int main(int argc,char * argv []){ if(argc&lt; 2){ COUT &LT;&LT; “错误:论据数量错误!至少给出一个论点...... \ n” &LT;&LT; ENDL; cout&lt;&lt; “\ n”&lt;&lt; “程序关闭...... \ n \ n”&lt;&lt; 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;
}
实际上,控制台窗口可能会持续0.5秒并再次关闭
它不会停留在我的任何一个cin.ignore();
也许它没有到达那里?
有人可以帮忙吗?
答案 0 :(得分:15)
除了处理命令行参数之外,您的程序不需要做任何特殊操作。当您将文件拖放到资源管理器中的应用程序时,它只会将文件名作为参数传递给程序。同样适用于多个文件。
如果你想要的只是一个文件名列表,那么只需遍历所有参数,随心所欲地做任何事情就可以完成。这将适用于零到几乎任意多的参数。
答案 1 :(得分:3)
也许你可以写一个这样的测试程序:
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;
}
看看你扔掉不同文件后会发生什么。
编辑:看看乔伊的回答。
答案 2 :(得分:1)
看到答案,直到最后一个问题看到这个答案的底部
所有拖放文件都可以argv[orderOfTheFile]
(orderOfTheFile
来自1-n),
然而 Windows如何创建该订单,现在 是一个真正的谜......
无论如何,我想在桌面上创建26个纯文本文件( * .txt ),从a.txt
到z.txt
,
现在,如果我将拖放,直接放在位于ArgsPrinter_c++.exe
驱动器上的C:\
上,
输出将与此类似:
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
我的ArgsPrinter_c++.exe
源代码:
#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;
}
我创建了一个简单的程序,只创建了类的一个sceleton,因此可以使用它,程序的主本身运行JUST FINE =&gt;如果您的程序退出太快,问题将出现在您的班级......
经过测试的源代码:
#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;
}