鱼C论坛

 找回密码
 立即注册
查看: 2236|回复: 2

这程序为什么老写入失败啊

[复制链接]
发表于 2012-6-21 20:09:14 | 显示全部楼层 |阅读模式
4鱼币
#include <iostream>
#include <string>
#include <fstream>
class StoreQuote
{
public:
std::string quote,speaker;
std::fstream fileOutput;
    StoreQuote();
    ~StoreQuote();
void inputQuote();
void inputSpeaker();
bool write();
};
StoreQuote::StoreQuote()
{
        fileOutput.open("test.txt",std::ios::app);
}
StoreQuote::~StoreQuote()
{
fileOutput.close();
}
void StoreQuote::inputQuote()
{
    std::getline(std::cin,quote);
}
void StoreQuote::inputSpeaker()
{
std::getline( std::cin,speaker);
}
bool StoreQuote::write()
{
  if(fileOutput.is_open())
  {
   std::cout<<quote<<"|"<<speaker<<std::endl;
   return true;
  }
   
else
{
  return false;
}
}
int main()
{
StoreQuote quote;
std::cout<<"请输入一句名言:"<<std::endl;
    quote.inputQuote();
std::cout<<"请输入作者名字:"<<std::endl;
quote.inputSpeaker();
if(quote.write())
{
    std::cout<<"成功写入文件"<<std::endl;
}
     else
{
        std::cout<<"写入失败"<<std::endl;
        return 1;
}
       return 0;
}

小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-6-21 21:58:52 | 显示全部楼层
fileOutput.open("test.txt",std::ios::app);
以追加方式打开文件,文件不存在就自然失败。
应该在.dsw所在的目录里事先建立文件test.txt
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-6-22 07:37:44 | 显示全部楼层
好的,谢谢版主
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-12 18:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表