Question

In: Computer Science

15.1 File IO Warm-up For this exercise, you will receive two string inputs, which are the...

15.1 File IO Warm-up

For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist."

You are required to create 4 functions:

void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is successfully written to outFileName."

int countChar (ifstream &inStream): returns number of character in input file.

int countWord(ifstream &inStream): returns number of words in input file.

int countLines(ifstream &inStream): returns number of lines in input file.

After calling each function, you have to reset the state flags and move the stream pointer to beginning of file. You can do that by closing and re-opening the file, or by calling member functions:

inStream.clear(); inStream.seekg(0);

Example:

Input: input.txt output.txt

Output:

Text with no white space is successfully written to output.txt.

Number of characters is: 154

Number of words is: 36

Number of lines is: 10

here is the code in C++:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void removeSpace(ifstream &inStream, ofstream &outStream);
int countChar (ifstream &inStream);
int countWords(ifstream &inStream);
int countLines (ifstream &inStream);

int main()
{
string input, output;
cin >> input >> output;

ifstream inStream(input);
ofstream outStream(output);

if(inStream.fail())
cout << "Input file does not exist.\n" ;
else
{
removeSpace(inStream, outStream);
cout << "Text with no white space is successfully written to outFileName.\n";
inStream.clear();
inStream.seekg(0, inStream.beg);

cout << "Number of characters is: " << countChar(inStream) << endl;
inStream.clear();
inStream.seekg(0, inStream.beg);

cout << "Number of words is: " << countWords(inStream) << endl;
inStream.clear();
inStream.seekg(0, inStream.beg);

cout << "Number of lines is: " << countLines(inStream) << endl;
inStream.close();
}

outStream.close();

return 0;
}

//define your functions here

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

void removeSpace(ifstream &inStream, ofstream &outStream);

int countChar(ifstream &inStream);

int countWords(ifstream &inStream);

int countLines(ifstream &inStream);

int main()

{

    string input, output;

    cin >> input >> output;

    ifstream inStream(input);

    ofstream outStream(output);

    if (inStream.fail())

        cout << "Input file does not exist.\n";

    else

    {

        removeSpace(inStream, outStream);

        cout << "Text with no white space is successfully written to "<<output<<".\n";

        inStream.clear();

        inStream.seekg(0, inStream.beg);

        cout << "Number of characters is: " << countChar(inStream) << endl;

        inStream.clear();

        inStream.seekg(0, inStream.beg);

        cout << "Number of words is: " << countWords(inStream) << endl;

        inStream.clear();

        inStream.seekg(0, inStream.beg);

        cout << "Number of lines is: " << countLines(inStream) << endl;

        inStream.close();

    }

    outStream.close();

    return 0;

}

void removeSpace(ifstream &inStream, ofstream &outStream){

    char ch;

    while(inStream>>ch){

        outStream<<ch;

    }

}

int countChar(ifstream &inStream){

    int count = 0;

    string line;

    while(!inStream.eof()){

        getline(inStream, line);

        count += line.size();

    }

    return count;

}

int countWords(ifstream &inStream){

    int count = 0;

    string word;

    while(!inStream.eof()){

        inStream>>word;

        count++;

    }

    return count;

}

int countLines(ifstream &inStream){

    int count = 0;

    string line;

    while(!inStream.eof()){

        getline(inStream, line);

        count++;

    }

    return count;

}


Related Solutions

For this exercise, you will receive two string inputs, which are the names of the text...
For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist." You are required to create 4 functions: void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is successfully written to outFileName."...
5.23 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string...
5.23 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string of their choosing. Output the string. (1 pt) Ex: Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have to fear is fear itself. (2) Complete the getNumOfCharacters() method, which returns the number of characters in the user's string. We encourage you to use a for loop in this function. (2 pts)...
6.40 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string...
6.40 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string of their choosing. Output the string. (1 pt) Ex: Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have to fear is fear itself. (2) Complete the GetNumOfCharacters() function, which returns the number of characters in the user's string. We encourage you to use a for loop in this function. (2 pts)...
python program You are going to write a program that takes two inputs: A string representing...
python program You are going to write a program that takes two inputs: A string representing a list of names of the following form: first name 1, last name 1, (nick name 1); first name 2, last name 2, (nick name 2); ... A string representing part of or the complete family name. You are going to output all nick names for those names where there is a partial or complete match of the family name. If no match was...
Design a GUI application for the Bank Account assignment you did in the File IO module....
Design a GUI application for the Bank Account assignment you did in the File IO module. It can be simple as a single user app with three buttons - checkBalance, Withdraw, Deposit - textbox for entering the amount. On Click of buttons, you can show the current balance in a dialog box. If you have more interest, you can expand it further according to your design of the application In python Please show codes and output. The calculator must be...
In Java please. Prompt the user for two string inputs. Perform the following actions on these...
In Java please. Prompt the user for two string inputs. Perform the following actions on these inputs, and print the result of each action: Part I: Concatenate the two strings into one. Compare the two strings and check whether they are the same (ignore the case). Look up the method equalsIgnoreCase() from Java API Convert all the characters in a string to uppercase Look up the method toUpperCase() from Java API Part II: Replace all the 'd' characters with ‘e'...
In this Exercise, you have to take a single string as input. Using this input string,...
In this Exercise, you have to take a single string as input. Using this input string, you have to create multiple queues in which each queue will comprise of separate word appeared in input string. At the end, you will again concatenate all queues to a single queue.s Example: String = “Data Structure and Algo” Q1 = D → a → t → a Q2 = S → t → r → u → c → t → u →...
Write a Python program, phone.py, that inputs a string of characters which represent a vanity telephone...
Write a Python program, phone.py, that inputs a string of characters which represent a vanity telephone number, e.g., 800-MYPYTHON, and prints the all numeric equivalent, 800-69798466. You should implement this using a loop construct to process each character from left to right. Build a new string that is the all numeric equivalent and then print the string.
1) You have a gyroscope and a string. Wind the string up and pull it fast...
1) You have a gyroscope and a string. Wind the string up and pull it fast to get the gyroscope spinning quickly. Try to turn the gyroscope upside down quickly. How does it feel different compared with doing the same thing when the gyroscope is not spinning? Finally, when it has slowed down a little (but not stopped) repeat the experiment. Does it feel the same when you try to flip the gyroscope at high speed versus low speed? Explain...
Which is FALSE about interneurons? Multiple Choice They sum excitatory and inhibitory synaptic inputs. They receive...
Which is FALSE about interneurons? Multiple Choice They sum excitatory and inhibitory synaptic inputs. They receive synaptic input from other neurons in the CNS. They deliver synaptic input on other neurons. They can transmit information between afferent neurons and efferent neurons. They make synapses on effector organs in the PNS.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT