#include<iostream>
#include<fstream>
#include <sstream>
#include<string>
using namespace std;
int main() {
ofstream ofile;
ofile.open("test.txt", ios::app);
ofile << "test" << endl;
ifstream ifile("test.txt");
if (!ifile) {
cout << "打开文件失败!" << endl;
}
char s[100];
char a;
string str;
//读文件的几种方法
// 第一种
while (ifile.get(a)) {
cout << a;
}
//第二种
//while (ifile.getline(s,20)) {
// cout << s<<endl;
//}
//第三种
//while (getline(ifile, str)) {
// cout << str << endl;
//}
ifile.close();
ofile.close();
return 0;
}
c++简单的文件读写示例
最新推荐文章于 2022-11-12 18:04:57 发布