#include<iostream>
#include<fstream>
using namespace std;
void remove_other_space(ifstream& in_stream, ofstream& out_stream);
int main()
{
ifstream fin;
ofstream fout;
cout<<"Begin editing files.\n";
fin.open("Exec6_6_in.dat");
if(fin.fail())
{
cout<<"Input file opening failed.\n";
exit(1);
}
fout.open("Exec6_6_out.dat");
if(fout.fail())
{
cout<<"Out put file opening failed.\n";
exit(1);
}
remove_other_space(fin, fout);
fin.close();
fout.close();
cout<<"End of editing files.\n";
return 0;
}
void remove_other_space(ifstream& in_stream, ofstream& out_stream)
{
char next;
in_stream.get(next);
while(!in_stream.eof())
{
while(next!=' ')
{
out_stream.put(next);
in_stream.get(next);
}
while(next==' ')
{
in_stream.get(next);
}
out_stream.put(' ');
}
}
我是这样写的,为什么结果会一直输出最后一个字符呢???
C++ 删除多余的空格
答案:1 悬赏:20 手机版
解决时间 2021-02-10 07:21
- 提问者网友:做自己de王妃
- 2021-02-09 17:51
最佳答案
- 五星知识达人网友:酒醒三更
- 2021-02-09 18:08
remove_other_space
嵌套里面的while用错了,这样会一直在转出不来
void remove_other_space(ifstream& in_stream, ofstream& out_stream)
{
char next;
in_stream.get(next);
while(!in_stream.eof())
{
if(next!=' ')
{
out_stream.put(next);
in_stream.get(next);
}
else
{
while(next==' ')
in_stream.get(next);
out_stream.put(' ');
}
}
}
嵌套里面的while用错了,这样会一直在转出不来
void remove_other_space(ifstream& in_stream, ofstream& out_stream)
{
char next;
in_stream.get(next);
while(!in_stream.eof())
{
if(next!=' ')
{
out_stream.put(next);
in_stream.get(next);
}
else
{
while(next==' ')
in_stream.get(next);
out_stream.put(' ');
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯