类似于Python在cmd中读取txt文件 example.py < data.txt
两个问题
1、C++应该在cmd里面输入什么来读取txt文件
2、C++的cpp文件里,代码里面应该怎么写读取文件的代码

C++程序怎么在windows的cmd命令行中读取txt文件
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- 广大菜鸟 2021-07-30 15:17关注
#include<iostream> #include<fstream> #include<string> using namespace std; void readFile(string filename); int main(int argc, char** argv) {// 第一个参数是本软件的exe /*for (int i = 0; i < argc; i++) // 查看有哪些命令 cout << argv[i] << endl;*/ if (argc == 3 && strcmp(argv[1],"-f")==0) { string filename = argv[2];// 读取文件显示 readFile(filename); } else { cout << "请按照格式输入信息:文件.exe -f 文件名" << endl; } } void readFile(string filename) { ifstream inFile(filename, ios::in); string temp; if (!inFile) { cerr << "File could not be open." << endl; } while (getline(inFile, temp)) { cout << temp; } inFile.close(); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用