#include <iostream>
#include <string>
#include <fstream> //文件读写
using namespace std;
class StoreQuote
{
public:
string quote;
string speaker;
ofstream fileOut; //!!!
StoreQuote(void);
~StoreQuote(); //析构器
void inputQuote(void);
void inputSpeaker(void);
bool saveQuote(void);
};
StoreQuote::StoreQuote(void)
{
fileOut.open("test.txt", ios::app); //打开文件
}
StoreQuote::~StoreQuote()
{
fileOut.close(); //关闭文件
}
void StoreQuote::inputQuote(void)
{
cout << "请输入名言:";
getline(cin, quote);
}
void StoreQuote::inputSpeaker(void)
{
cout << "请输入作者:";
getline(cin, speaker);
}
bool StoreQuote::saveQuote(void)
{
if (fileOut.is_open())
{
fileOut << speaker << ":" << quote << endl; //写入文件
return true;
}
else
{
return false;
}
}
int main()
{
StoreQuote sq;
sq.inputQuote();
sq.inputSpeaker();
if (sq.saveQuote())
{
cout << "写入成功!" << endl;
}
else
{
cout << "写入失败!" << endl;
}
return 0;
}