C++读写文件

C++读写文件

#include 
#include 
#include 
#include 
#include 
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;
    }
    //1.读数据
    char buf[1024]={0};
    while (ifs>>buf)
    {
        cout<<buf<<endl;
    }
    ifs.close();
}
int main() {

    test05();
    test06();
    return 0;
}

在这里插入图片描述

你可能感兴趣的:(C++,c++)