鱼C论坛

 找回密码
 立即注册
查看: 2356|回复: 1

程序getline文件错误想问问为什么

[复制链接]
发表于 2012-6-18 20:02:49 | 显示全部楼层 |阅读模式
1鱼币
#include <iostream>
#include <string>
#include <fstream>
class StoreQuote
{
public:
std::string quote,speaker;
std::ofstream fileOutput;
    StoreQuote();
    ~StoreQuote();
public: int 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;
}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-6-25 11:31:50 | 显示全部楼层
本帖最后由 MewCatcher 于 2012-6-25 11:34 编辑

std::getline(std::cin,quote);
你用cin干嘛?
用那个文件变量fileOutput
我有时候这样用:
#include <fstream>
using namespace std;
( ... )
    fstream OP;
    OP.open( "cca0001.dsf", ios_base::in );
    while( !OP.eof( ) ) {
        char Temp[ 2000 ];
        OP.getline( Temp, 1999, '\n' );
    }
( ... )

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-16 15:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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