从命令行解析输入和输出文件名作为参数
parsing input and output filename as argument from command line
我是 C++ 的新手,在一个项目中我需要使用命令行参数。
我阅读了命令行参数,即包括
int main(int argc, char** argv)
{
}
但我在源文件中声明我的文件名时遇到问题。
我在源文件 (file_process.cpp) 中将我的输入和输出文件名声明为
const char iFilename[] ;
const char oFilename[] ;
将函数(使用输入文件 - iFilename 并处理 oFilename 中的输出)定义为
void file_process::process(iFilename[], oFilename[])
{
body...
}
并且在主要方法中为:
int main(int argc, char** argv) {
iFilename[] = argv[1];
oFilename[] = argv[2];
file_process::process(iFilename[], oFilename[]);
}
早些时候我硬编码了文件名以在 main 方法中不带参数地测试我的程序,并在源文件中声明变量 (file_process.cpp) 为:
const char iFilename[] = "input_file.pdf";
const char oFilename[] = "output_file.txt";
它工作正常,但是当我尝试从命令行获取参数时,我无法编译它。
在 C++ 中这样做是否正确?我使用 c# 并在源文件中简单地声明如下:
string iFilename = args[0];
string oFilename = args[1];
有效。
我
这是一种方法:
int main(int argc, char** argv)
{
assert(argc >= 3);
const std::string iFilename = argv[1];
const std::string oFilename = argv[2];
file_process::process(iFilename, oFilename);
}
而你 file_process::process
可能是:
void file_process::process(const std::string& iFilename, const std::string& oFilename)
{
body...
}
我是 C++ 的新手,在一个项目中我需要使用命令行参数。 我阅读了命令行参数,即包括
int main(int argc, char** argv)
{
}
但我在源文件中声明我的文件名时遇到问题。
我在源文件 (file_process.cpp) 中将我的输入和输出文件名声明为
const char iFilename[] ;
const char oFilename[] ;
将函数(使用输入文件 - iFilename 并处理 oFilename 中的输出)定义为
void file_process::process(iFilename[], oFilename[])
{
body...
}
并且在主要方法中为:
int main(int argc, char** argv) {
iFilename[] = argv[1];
oFilename[] = argv[2];
file_process::process(iFilename[], oFilename[]);
}
早些时候我硬编码了文件名以在 main 方法中不带参数地测试我的程序,并在源文件中声明变量 (file_process.cpp) 为:
const char iFilename[] = "input_file.pdf";
const char oFilename[] = "output_file.txt";
它工作正常,但是当我尝试从命令行获取参数时,我无法编译它。
在 C++ 中这样做是否正确?我使用 c# 并在源文件中简单地声明如下:
string iFilename = args[0];
string oFilename = args[1];
有效。 我
这是一种方法:
int main(int argc, char** argv)
{
assert(argc >= 3);
const std::string iFilename = argv[1];
const std::string oFilename = argv[2];
file_process::process(iFilename, oFilename);
}
而你 file_process::process
可能是:
void file_process::process(const std::string& iFilename, const std::string& oFilename)
{
body...
}