C++简单的文件读写操作
2016-01-10 00:00
429 查看
#include <iostream> #include <sstream> #include <fstream> #include <vector> #include <typeinfo> //用于抛出异常 throw std::bad_cast();
class StudentInfo{ private: std::string studentNumber; std::string name; std::string className; double psOne; double psTwo; public: StudentInfo()=default; StudentInfo(const std::string& number, const std::string& sName, const std::string& cName, const double& first, const double& second); ~StudentInfo()=default; friend std::istream& operator>>(std::istream& is, StudentInfo& stuInfo); //重载的>>(输入运算符)注意这里是友元. friend std::ostream& operator<<(std::ostream& os, const StudentInfo& stuInfo); //重载的<<(输出运算符)注意这里也是友元. //关于运算符的重载只有运算符为 =(赋值), [](下标), ->(箭头)的时候为成员. //但是像 +, -, *, /一般情况下写成成员. const std::string getInfo()const; };
StudentInfo::StudentInfo(const std::string& number, const std::string& sName, const std::string& cName, const double& first, const double& second) :studentNumber(number), name(sName), className(cName), psOne(first), psTwo(second) { std::cout<<"create success"<<std::endl; }
const std::string StudentInfo::getInfo()const { return (this->studentNumber+" "+this->name+" "+this->className+" "+std::to_string(this->psOne)+" "+std::to_string(this->psTwo)); //std::to_string()是把数字无论是float,double, int, long int, unsigned int...转换为string的一个标准库函数. }
std::istream& operator>>(std::istream& is, StudentInfo& stuInfo) { is>>stuInfo.studentNumber; is>>stuInfo.name; is>>stuInfo.className; is>>stuInfo.psOne; is>>stuInfo.psTwo; return is; //重载的输入运算符返回类型必须是istream, 注意返回也是引用. }
std::ostream& operator<<(std::ostream& os, const StudentInfo& stuInfo) { os<<stuInfo.studentNumber<<" "<<stuInfo.name<<" "<<stuInfo.className<<" "<<stuInfo.psOne<<" "<<stuInfo.psTwo<<std::endl; return os; //返回类型也必须是引用. }
class RWFile{ private: std::string textName; //txt文件的名字. std::vector<std::string> textContent; //txt文件的内容. StudentInfo stuInfo; //学生的学号,名字.... public: RWFile(const std::string& tName=std::string("")); ~RWFile(); void printText(); void writeText(); };
RWFile::RWFile(const std::string& tName) :textName(tName) { if(this->textName.empty()){ std::cout<<"输入txt的名字: "<<std::endl; std::cin>>this->textName; } }
RWFile::~RWFile() { this->textContent.clear(); std::cout<<"destroy RWFile"<<std::endl; }
void RWFile::printText() { std::string contents; if(this->textName.empty()){ std::cout<<"输入txt的名字: "<<std::endl; } //std::fstream fileRW(this.textName); //这么写等价于下面. std::fstream fileRW; fileRW.open(this->textName, std::fstream::in|std::fstream::out|std::fstream::binary); //in(指的是以写入的形式打开), out(指的是以读的形式打开) if(fileRW.is_open()){ while(getline(fileRW, contents)){ //注意这里这里fileRW的类型是fstream,因此可以当做std::cin来使用. 把txt中的内容一行一行的放入到vector; this->textContent.push_back(contents); } this->textContent.shrink_to_fit();//这一句可有可无.为了省内存. for(auto str : this->textContent){ std::cout<<str<<std::endl; } }else{ std::cout<<"filed to open."<<std::endl; throw std::bad_cast(); } fileRW.close(); }
void RWFile::writeText() { std::cout<<"学号"<<" "<<"姓名 "<<"课程名字 "<<"平1 "<<"平2 "<<std::endl; std::fstream fileRW(this->textName); /*while(std::cin>>stuInfo){ } */ std::cin>>this->stuInfo; if(fileRW.is_open()){ std::string str = stuInfo.getInfo(); fileRW<<str; //把刚刚输入的内容写到文件中. }else{ std::cerr<<"filed to open"<<std::endl; throw std::bad_cast(); } fileRW.close(); }
int main() { RWFile myFile; myFile.writeText(); myFile.printText(); return 0; }
相关文章推荐
- Effective C++: 数字类型与类型提升.
- C语言中memset函数详解
- 数组的首地址和数组的第一个元素的首地址和数组做函数参数的退化问题
- 学习C++的建议
- Sicily 2038. String Reversion
- C语言实现字节流与十六进制字符串的相互转换
- 《C语言及程序设计》第32讲实践项目
- vs使用Visual Leak Detector检测内存泄漏的某些注意事项
- 用OpenSSL 做HMAC(C++)
- 搜索强连通分量_Tarjan算法_C++实现
- C++ 之 static 关键字
- Sicily 1561. PRIME
- C语言几个文件操作函数fopen(),fseek(),fscanf(),fwrite()
- C语言操作MYSQL
- C#调用C++ DLL 中定义的函数
- (C++编程规范第17条)避免使用”魔数“
- C++学习笔记41——重载操作符之下标操作符
- C++ DirectX 游戏开发初级视频教程 05 资源下载链接
- struct 结构体笔记
- C++中的new/delete运算符与operator new/operator delete函数