You are on page 1of 1

To Remove the extra spaces from a text file

#include<fstream.h>
#include<stdio.h>
#include<conio.h>

void main()
{
ofstream fout("TEST.TXT");
fout<<"This is to remove extra spaces.";
fout.close();
fout.open("TMP.TXT");
ifstream fin("TEST.TXT");
char ch;
int k=1;
clrscr();
cout<<"Text in the file is :";
while(fin)
{
fin.get(ch);
if(ch!=' ')
fout<<ch;
else if(ch==' '&&k==1)
{
fout<<ch;
k=0;
}
if(k==0&&ch!=' ')
k=1;
cout<<ch;
}
fout.close();
fin.close();
remove("TEST.TXT");
rename("TMP.TXT","TEST.TXT");
fin.open("TEST.TXT");
cout<<"\n Final text in the file is :\n";
while(fin)
{
fin.get(ch);
cout<<ch;
}
getch();
}

OUTPUT

You might also like