C++程序怎么在windows的cmd命令行中读取txt文件

C++程序怎么在windows的cmd命令行中读取txt文件

问题描述:

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

img

#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();
}