文件
#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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示