c++的ifstream和ofstream读写类对象
来源:互联网 发布:广州cnc编程招聘 编辑:程序博客网 时间:2024/05/21 10:00
#include <iostream>#include <fstream>#include <string>using namespace std;class Student{public://有元声明最后放在public里面,不知道为啥friend istream& operator>>(istream&is, Student&st);friend ostream& operator<<(ostream&os, const Student&st);Student() = default;Student(string na, int sc){name = na;score = sc;}~Student() = default;private:string name;int score;};istream& operator>>(istream&is, Student&st)//ifstream是isream的子类,也能作为函数的参数{is >> st.name;is>>st.score;return is;}ostream& operator<<(ostream&os, const Student&st)//ofstream是osream的子类,也能作为函数的参数{os << st.name << " " << st.score << endl;return os;}int main(){ ////二进制文件操作//写文件//ofstream fout("student.dat",ios::binary);//能自动创建文件//Student s1("李明",100);//fout.write((char*)&s1,sizeof(s1));//fout.flush();//fout.close();//读文件//ifstream fin("student.dat",ios::binary);//Student s2; //fin.read((char*)&s2, sizeof(s2));//fin.close(); ////文本文件操作//写文件 ofstream fout("student.txt");//能自动创建文件Student s1("李明", 100);fout << s1;fout.flush();fout.close();//读文件 ifstream fin("student.txt");Student s2;fin >> s2;fin.close();return 0;}C++的ifstream和ofstream读写二进制文件只能用read和write函数吗?用<<和>>即使指定了binary方式,也不能读写二进制文件
1 0
- c++的ifstream和ofstream读写类对象
- 【c++】ofstream和ifstream
- ofstream和ifstream 读写文件
- c++ifstream和ofstream详解
- c++ifstream和ofstream详解
- C++ifstream和ofstream用法
- C++11 ifstream和ofstream读写二进制文件
- ofstream和ifstream的用法
- ofstream和ifstream的用法
- ifstream&&ofstream的.csv文件读写
- C++ ofstream和ifstream详细用法
- 【c/c++】C++文件读写详解(ofstream,ifstream,fstream)
- C++中文件ifstream和ofstream流类的操作
- [c++]C++文件读写详解(ofstream,ifstream,fstream)
- ofstream 和ifstream的具体用法
- ofstream 和ifstream的具体用法
- ofstream 和ifstream的具体用法
- ofstream 和ifstream的具体用法
- SQL索引一步到位
- 如何在64位的win7个人电脑上部署spark开发环境
- spring的工作原理
- AOP切面实现动态数据源(含代码)
- xml基本概念总结
- c++的ifstream和ofstream读写类对象
- log4j2详解
- 【精粹】如何打造一个高逼格的云运维平台?
- SVN使用手册
- bzoj 2803 [Poi2012]Prefixuffix hash
- mysql索引总结----mysql 索引类型以及创建
- Android中的Handler机制
- 输出一个整数的每一位
- 【NOIp 2012】【线段树】借教室