您的位置:首页 > 编程语言 > C语言/C++

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: