1.效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/19a778355ed349a7965f7453a1bda51d.gif#pic_center)
2.代码: cat.cc
#include <fstream>
#include <iostream>
#include <string>
int main(int argc, char const *argv[]) {
if (argc != 2) {
std::cout << "参数不正确,请正确输入文件路径!" << std::endl;
exit(1);
}
std::string filePath = argv[1];
std::ifstream istrm(filePath, std::ios::in);
if (!istrm.is_open()) {
std::cout << "failed to open " << filePath << '\n';
std::cout << "请确认文件路径是否正确!" << std::endl;
exit(1);
}
std::string lineStr;
while (std::getline(istrm, lineStr)) {
std::cout << lineStr << std::endl;
}
return 0;
}
3.测试
- 1.编译: g++ cat.cc -o cat
- 2.运行:./cat cat.cc(需要查看的文件)