|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
使用软件vs2013!代码如下:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class StoreQuote
{
public:
string quote, speaker;
ofstream flieOutput;
StoreQuote();
~StoreQuote();
void inputQuote();
void inputSpeaker();
bool write();
};
StoreQuote::StoreQuote()
{
flieOutput.open("text.txt", ios::app);
}
StoreQuote::~StoreQuote()
{
flieOutput.close();
}
void StoreQuote::inputQuote(){
getline(cin, quote);
}
void StoreQuote::inputSpeaker(){
getline(cin, speaker);
}
bool StoreQuote::write(){
if (flieOutput.is_open())
{
flieOutput << quote << "|" << speaker << endl;
return true;
}
else { return false; }
}
int main()
{
StoreQuote quote; //这边显示不能使用类型名,为什么??
cout << "please input a quote: " << endl;
quote.inputQuote();
cout << "please input the speaker:" << endl;
quote.inputSpeaker();
if (quote.write())
{
cout << "成功写入文件";
}
else{
cout << "写入文件失败";
return 1;
}
return 0;
}
|
|