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.
How can I write java program code that do reverse, replace, remove char from string without...
How can I write java program code that do reverse, replace, remove char from string without using reverse, replace, remove method. Only string method that I can use are length, concat, charAt, substring, and equals (or equalsIgnoreCase).
Write an assembly language program to input a string from the user. Your program should do...
Write an assembly language program to input a string from the user. Your program should do these two things: ***** I AM POSTING THIS QUESTION FOR THE THIRD TIME NOW,I WANT IT DONE USING INCLUDE IRVINE32.INC***** 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be:...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
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.
I need to write java program that do replace first, last, and remove nth character that...
I need to write java program that do replace first, last, and remove nth character that user wants by only length, concat, charAt, substring, and equals (or equalsIgnoreCase) methods. No replace, replaceFirst, last, remove, and indexOf methods and letter case is sensitive. if user's input was "be be be" and replace first 'b' replace first should do "e be be" replace last should do "be be e" remove nth character such as if user want to remove 2nd b it...
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 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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT