c++读取文件时怎么换行读
可以使用 getline 函数来读取文件中的一行数据,并自动识别换行符。例如:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream infile("example.txt");
string line;
while (getline(infile, line)) {
cout << line << endl; // 输出每一行数据
}
return 0;
}
这样可以逐行读取文件中的数据,并在每一行后输出一个换行符,从而实现换行读取。
原文地址: https://www.cveoy.top/t/topic/gIAw 著作权归作者所有。请勿转载和采集!