如何启用在 *.exe 上拖动文件并将其作为参数获取?
我需要做什么才能让我的程序使用已拖放到其图标上的文件作为参数?
我当前的 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 中,然后从我的应用程序中处理它们。
编辑:
传入参数用作文件名,在构造函数中构造。
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 < 2) { 库特 << “错误:参数数量错误!至少给出一个参数...\n” <<结束; 计算<< “\n”<< “程序已关闭...\n\n”<<结束; 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();
上,也许它没有到达那里?
有人可以帮忙吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了处理命令行参数之外,您的程序不需要做任何特殊的事情。当您将文件拖放到资源管理器中的应用程序时,它只会将文件名作为参数传递给程序。对于多个文件也是如此。
如果您期望的只是一个文件名列表,那么只需迭代所有参数,对它们执行任何您想要的操作即可完成。这适用于零到几乎任意多个参数。
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.
也许您可以编写一个这样的测试程序:
然后看看向它扔不同的文件后会发生什么。
编辑:看看乔伊的回答。
Maybe you could write a test program like this:
And see what happens after you throw different files at it.
EDIT: Just look at Joey's answer.
主要问题的答案
要查看最后一个问题的答案,请参见此答案的底部
所有拖放的文件都可以通过
argv[orderOfTheFile]
(orderOfTheFile
是从 1-n),但是 Windows 如何创建该顺序,现在这 是一个真正的谜...
无论如何,假设我将创建 26 个纯文本文件 ( *. txt ),从桌面上的
a.txt
到z.txt
,现在,如果我将它们拖放到到直接位于
C:\
驱动器上的ArgsPrinter_c++.exe
上,输出将类似于:
我的
ArgsPrinter_c++.exe
源代码:你的最后一个问题
我创建了一个简单的程序,它只创建你的类的一个sceleton,以便可以使用它,并且< strong>该程序的主要本身运行得很好 =>如果你的程序退出得太早,问题将出在你的类中......
经过测试的源代码:
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
toz.txt
on my Desktop,now if I would drag&dropped them on my
ArgsPrinter_c++.exe
located directly onC:\
drive,an output would be similar to this:
My
ArgsPrinter_c++.exe
source code: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: