C++读写文件
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;
void test05()
{
ofstream ofs;
ofs.open("D:\\test02\\ttt.txt",ios::out);
ofs<<"姓名:小明"<<endl;
ofs<<"性别:男"<<endl;
ofs<<"年龄:18"<<endl;
ofs.close();
cout<<"over"<<endl;
}
void test06()
{
ifstream ifs;
ifs.open("D:\\test02\\ttt.txt",ios::in);
if (!ifs.is_open())
{
cout<<"文件打开失败!"<<endl;
return;
}
char buf[1024]={0};
while (ifs>>buf)
{
cout<<buf<<endl;
}
ifs.close();
}
int main() {
test05();
test06();
return 0;
}