Question

In: Computer Science

Write a program to remove extra blanks from text files. Your program should replace any string...

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++

Solutions

Expert Solution

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


Related Solutions

Write a C program that can search for a string within a text file, replace it...
Write a C program that can search for a string within a text file, replace it with another string and put results in a new file.
Creating a list/tuple 4. Write a program to accept a string from the user, then replace...
Creating a list/tuple 4. Write a program to accept a string from the user, then replace the first letter with the last letter, the last letter with the middle letter and the middle letter with the first letter. E.g. “Billy” -> “yiBll”.
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Design a shell program to remove all the shell programming files ending with sh on your...
Design a shell program to remove all the shell programming files ending with sh on your home directory when a SIGINT signal is received.
Write a python program function to check the frequency of the words in text files. Make...
Write a python program function to check the frequency of the words in text files. Make sure to remove any punctuation and convert all words to lower case. If my text file is like this: Hello, This is Python Program? thAt chEcks% THE freQuency of the words! When is printed it should look like this: hello 1 this 1 is 1 python 1 program 1 that 1 checks 1 the 2 frequency 1 of 1 words 1
Write a GUI-based program that allows the user to open, edit, and save text files. The...
Write a GUI-based program that allows the user to open, edit, and save text files. The GUI should include a labeled entry field for the filename and multi-line text widget for the text of the file. The user should be able to scroll through the text by manipulating a vertical scrollbar. Include command buttons labeled Open, Save, and New that allow the user to open, save and create new files. The New command should then clear the text widget and...
Write a simple java program that produces any text art ("ASCII art") picture. Your program can...
Write a simple java program that produces any text art ("ASCII art") picture. Your program can produce any picture you like, with the following restrictions: • The picture should consist of between 3 and 200 lines of output, with no more than 100 characters per line. • The code must use at least one for loop or static method but should not contain infinite loops.
write a c++ program to read two matrices with any size. Your program should have at...
write a c++ program to read two matrices with any size. Your program should have at least the following functions Main() Read a Matrix Add two matrices Subtract two matrices multiply two matrices display a matrice
. Write a program which encodes any string using the XOR instruction.  Test it using your <first...
. Write a program which encodes any string using the XOR instruction.  Test it using your <first name last name> in the data segment to produce cipher text and then decode using the program to get plain text.  Use the last two digits of your student id as the key.  Print plane text from the data segment, print the cipher text, and then print the plain text upon execution.  Submit the asm/list file and screenshots that shows the output of your code. What are...
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT