In: Computer Science
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or put puncuations marks at the beginning of a new line.
step: 1 of 7
Problem Plan:
• Include the required header files into the program.
o Define the "main()" function to format a file by removing blank lines multiple lines, lines of equal length print the output in an another file.
o Initialize string variable "f_nme" for file name.
o message for user to enter the file name
o Get the value for file name from the user
o Use "ifstream" for input file
o Get the value for output file name from the user
o Use "ofstream" for output file
o Using assert function to check whether the file is open
o Initialize the required variables
o get the width from the user
o get the string from the input file one by one
o Use "for" loop to clean the file
• Assign the string value to next string
• If the string length is greater than given line width
• The output file does not contain the given width value
• Get the next string from the file
• If end of file is reached then print in the output file
• Check for the punctuation in the string.
• Assign the string to the first value
• If the line length is 0 then assign the string to the line
• Otherwise print space
• Print the results to the output file
• Close the files
step: 2 of 7
Program:
/*********************************************************** Program to format a file by removing blank lines, *
* multiple lines, lines of equal length print the output *
* in an another file *
**********************************************************/
//include header files
#include
#include
#include
#include
#include
using namespace std;
//main function
int main()
{
//string variable for filename
string f_nme;
//message for user to enter the file name
cout << "Please enter the unformated file name: ";
//get file name from user
cin >> f_nme;
//Use ifstream for input file
ifstream i_F(f_nme.data());
assert(i_F.is_open());
//message for user to enter output filename
cout << "please enter where you want to store the output: ";
//get output filename form user
cin >> f_nme;
// offstream for open output file
ofstream o_F(f_nme.c_str());
assert(o_F.is_open());
//request the user for maximum length
cout << "Enter max line length: ";
//initialise required variables
int wdh_frmt;
string strng, nextstrng, line;
//get the width from the user
cin >> wdh_frmt;
//get the string from the input file one by one
i_F >> nextstrng;
//loop to clean the file
for (;;)
{
//assign the string value to next string
strng = nextstrng;
//if the string length is greater than given line //width
if (strng.length() > wdh_frmt)
{
step: 3 of 7
//the output file does not contain the given //width valuecerr << strng << " not possible line length " << wdh_frmt
<< " -- execution is terminated.\n";
return 0;
}
//get the next string from the file
i_F >> nextstrng;
//if end of file is reached
if (i_F.eof())
{
//line length is greater than 0
if (line.length() > 0)
//print in the output file
o_F << line << endl;
break;
}
//check for the punctuation in the string
while (nextstrng.length() > 0 && ispunct(nextstrng[0]))
{
//assign the string to the first value
strng += nextstrng[0];
//erase the next string value
nextstrng = nextstrng.erase(0, 1);
}
//if the line length is 0
if (line.length() == 0)
//assign the string to the line
line = strng;
//otherwise print space
else if (line.length() + strng.length() < wdh_frmt)
line += " " + strng;
//print the results to the output file
else
{
o_F << line << endl;
line = strng;
}
}
//close the files
i_F.close();
o_F.close();
}
step: 4 of 7
Sample Input:
f1.txt
Maria is studying to become an immigration lawyer.
At her university alone there are more than a thousand undocumented students;
so many, they have established a support centre.
Cities like Los Angeles have vowed to fight to defend immigrant communities
step: 5 of 7
Sample Output:
Please enter the unformated file name: f1.txt
please enter where you want to store the output: f2.txt
Enter max line length: 15
step: 6 of 7
Sample Output file (f2.txt):
Maria is
studying to
become an
immigration
lawyer. At her
university
alone there are
more than a
thousand
undocumented
students; so
many, they have
established a
support centre.
Cities like Los
Angeles have
vowed to fight
step: 7 of 7
to defendimmigrant
communities