无法把数据保存进文件
程序代码:
#include <iostream> #include <vector> #include <list> #include <algorithm> #include <functional> #include <iterator> #include <fstream> #include <string> class StoreQuote { public: string quote, speaker; ofstream fileOutput; StoreQuote(); ~StoreQuote(); void inputQuote(); void inputSpeaker(); bool write(); }; //创建文件,并且打开 StoreQuote::StoreQuote() { fileOutput.open("text.txt", ios::app); } //关闭文件 StoreQuote::~StoreQuote() { fileOutput.close(); } //输入文本 void StoreQuote::inputQuote() { getline(cin, quote); //cin >> qsort >> endl; } void StoreQuote::inputSpeaker() { getline(cin, speaker); } bool StoreQuote::write() { if(fileOutput.is_open()) { fileOutput << quote << "|" << speaker << "\n"; return true; } else { return false; } } int main() { StoreQuote quote; cout << "请输入一句话: " << endl; quote.inputQuote(); cout << "请输入作者: " << endl; quote.inputSpeaker(); if( &StoreQuote::write) { cout << "成功写入文件!" << endl; } else { cout << "写入文件失败!" << endl; return 1; } return 0; }
麻烦大家帮忙看一下问题在哪,谢谢大家!