In: Computer Science
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
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;
}