为什么如下程序用getline会出现错误,哪位大哥帮忙看看。

fczqx 2009-04-08 04:51:11
为什么如下程序红色处用getline会出现错误,哪位大哥帮忙看看,想用getline做,应该如何改啊?非常感谢!
#include "123.h"
#include <fstream>
#include<iostream>
#include <iomanip>
using namespace std;

void get_data()
{char ch;
ifstream infile("E:\\test.txt",ios::in);//input data
if(!infile)
{cerr<<"open error!"<<endl;
exit(1);
}
ofstream outfile("E:\\result.txt");//output data
if(!outfile)
{cerr<<"error"<<endl;
exit(1);
}
while([color=#FF0000]infile.get(ch))[/color]{

outfile.put(ch);
cout<<ch;

}
cout<<endl;
infile.close();
outfile.close();}
int main()
{
get_data();
return 0;}
...全文
89 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fczqx 2009-04-09
  • 打赏
  • 举报
回复
没人来了么?
donle1000 2009-04-08
  • 打赏
  • 举报
回复

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

void get_data()
{
char ch[256]; //getline获取行,不能用单个字符接收
ifstream infile("data.txt",ios::in);//input data
if(!infile)
{
cerr <<"open error!" <<endl;
exit(1);
}
ofstream outfile("file.txt");//output data
if(!outfile)
{
cerr <<"error" <<endl;
exit(1);
}
while(infile.getline(ch,80)) //读取长度
{

//outfile.put(ch);
cout <<ch << endl;

}
cout <<endl;
infile.close();
outfile.close();
}
int main()
{
get_data();
return 0;}
sunzuyi 2009-04-08
  • 打赏
  • 举报
回复
你想用getline实现什么?将从test文件里读取数据改成你手动输入么?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧