问题描述
我需要从命令行参数读取文件,然后对其内容进行一些操作。我已经编写了一个小代码对其进行测试。
#include <fstream>
#include <iostream>
#include <string>
int main(int argc,char* argv[]) {
using std::cout;
using std::endl;
using std::string;
if (argc != 2) {
cout << "Usage: ./a.out Filename"
<< endl;
return 1;
}
std::fstream input(argv[1],std::ios_base::in);
string word;
while(getline(input,word)) {
cout << word;
}
return 0;
}
如果我通过./a.out filename.txt
在终端中运行此代码,则该代码可以正常工作
我正在用CLion编写代码,因此我需要在CLion中提供文件名/文件路径作为命令行参数。
我尝试在运行->编辑配置->程序参数中将文件名添加为参数,但据我了解,CLion将filename.txt
视为简单参数,而不是文件名。我也尝试提供文件的路径,但是没有运气,当我按run
时,CLion仍然无法读取文件内容。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)