Question

In: Computer Science

Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...

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.

Solutions

Expert Solution

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


Related Solutions

Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
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
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT