文件

复制代码
#include<iostream>
using namespace std;
#include<fstream>
test01(){
    ofstream ofs;
    ofs.open("test.txt",ios::out);
    ofs<<"姓名"<<endl;
    ofs<<"性别"<<endl; 
    ofs.close();
}
test02(){
    ifstream ifs;
    ifs.open("test.txt",ios::in);
    string bug;
    while(getline(ifs,bug)){
        cout<<bug<<endl;
    }
    ifs.close();
}
int main(){
    test01();
    test02();
    system("pause");
    return 0;
}
复制代码

 二进制文件

复制代码
#include<iostream>
using namespace std;
#include<fstream>
class Person{
    public:
        char m_Name[64];//
        int m_Age;
};
void test01(){
    //1.包含头文件
    //2.创建流对象
    ofstream ofs;
    //3.打开文件
    ofs.open("person.txt",ios::out| ios::binary);
    //4.写文件
    Person p={"zhangsan",18};
    ofs.write((const char *)&p,sizeof(Person));
    //5.关闭文件
      ofs.close();
}
void test02(){
    ifstream ifs("person.txt",ios::in | ios::binary);
    if(!ifs.is_open()){
        cout<<"打开失败"<<endl;
        return ; 
    }
    Person p;
    ifs.read((char *)&p,sizeof(Person));
    cout<<"xingming:"<<p.m_Name<<"age:"<<p.m_Age<<endl;
    ifs.close();
}
int main(){
    test01();
    test02();
    system("pause");
    return 0;
}
复制代码

 

posted @   艾鑫4646  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示