标签:std close line while cstring namespace algo fstream fst
使用ifstream 然后对C++进行文件的读取
头文件
>#include<cstdio>
>#include<cmath>
>#include<cstring>
>#include<cstdlib>
>#include<algorithm>
>#include<iostream>
>#include<string>
>#include<fstream>
>using namespace std;
读取代码段
int main() {
string buffer, filepath = "test.txt";
ifstream myfile(filepath.c_str());
//等同于 ifstream myfile("test.txt");
while(getline(myfile, buffer)) {
cout << buffer << endl;
}
myfile.close();
return 0;
}
写入代码段
int main() {
string filepath = "test.txt";
//ofstream myfile("test.txt");
ofstream myfile(filepath.c_str());
myfile << "hbhh" << endl;
myfile << "aaaaa" << endl;
myfile.close();
return 0;
}
标签:std close line while cstring namespace algo fstream fst
原文地址:https://www.cnblogs.com/zoey686/p/11539766.html