In: Computer Science
Write a program to remove extra blanks from text files. Your program should replace any string of two or more blanks with one single blank. It should work as follows:
* Create a temporary file.
* Copy from the input file to the temporary file, but do not copy
extra blanks. * Copy the contents of the temporary file back into
the original file.
* Remove the temporary file.
Your temporary file must have a different name than any existing file, so that no files other than the input file are changed. Your program should ask the user for the name of the file to be edited, but should not request a name for the temporary file. Instead, generate the name within the program. You can generate the temporary file using any strategy that is clear and efficient. For example, you could start with a name like TempX and check if that name already exists. If it doesn't, you can use that name for the temporary file; if it does exist, append additional random letters to the filename until you find a name that isn't already used.
Language is in C++
Please find the code below.
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <sstream>
#include <cmath>
using namespace std;
int main( )
{
ifstream infile;
string line;//for read line
char fileName[20];
cout<<"Enter file name : ";
cin>>fileName;
string tempFile ="temp";
while(true){
cout<<"Opening file
"<<tempFile;
infile.open (tempFile+".txt");
//name of file here. plz mention Complete path if file is not at
root
cout<<"File found. Creating
new file"<<endl;
if (infile.is_open())
{
int randN =
rand()%10;
stringstream
sstm;
sstm <<
tempFile << randN;
tempFile =
sstm.str();
infile.close();
}else{
break;
}
}
infile.open (fileName); //name of file here. plz
mention Complete path if file is not at root
if (infile.is_open()) //if file opened
{
ofstream writer (tempFile+".txt");
//printing data to out file
while( getline(infile, line,'\n') )
{ //get row from text file
stringstream
ss(line); //stream to other variable
while(ss>>line){ //row delimeter by space
writer<<line<<" ";
}
writer<<endl;
}
infile.close(); //close file
cout<<"File scan
done........"<<endl;
}
else //if file not found show the below message
{
cout << "Sorry, we could not
find the file.Please try again!!!!" << endl;
}
//copy content to orginal file
infile.open (tempFile+".txt"); //name of file here.
plz mention Complete path if file is not at root
if (infile.is_open()) //if file opened
{
ofstream writer (fileName);
//printing data to out file
while( getline(infile, line,'\n') )
{ //get row from text file
writer<<line<<endl;
}
infile.close(); //close file
cout<<"Copy done to input
file";
}
else //if file not found show the below message
{
cout << "Sorry, we could not
find the file.Please try again!!!!" << endl;
}
//removing file
char fileName2[100];
stringstream ss(tempFile+".txt");
ss>>fileName2;
remove(fileName2);
return 0;
}
output:
where file content before:
once upon a time there were three little pig who lived alone
with a big pig all are pig and pig
wat how where jmm
sdfj sdf ds sdf dsf sdf
after:
once upon a time there were three little pig who lived alone
with a big pig all are pig and pig
wat how where jmm
sdfj sdf ds sdf dsf sdf