C++ 删除多余的空格
#include<iostream>#include<fstream>usingnamespacestd;voidremove_other_space(ifstream&...
#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(' ');
}
}
我是这样写的,为什么结果会一直输出最后一个字符呢??? 展开
#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(' ');
}
}
我是这样写的,为什么结果会一直输出最后一个字符呢??? 展开
1个回答
展开全部
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(' ');
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询