Please paste your code and screenshot of output Codes below: COPY FILE // File:
ID: 3603181 • Letter: P
Question
Please paste your code and screenshot of output
Codes below:
COPY FILE
// File: CopyFile.cpp
Countchars code:
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#define ENDFILE "CTRL-Z"
int main()
{
const char NWLN = ' '; // newline character
char next; // next character in current line
int charCount; // number of characters in current line
int totalChars; // count of total characters
int lineCount; // keeps track of number of lines in file
lineCount = 0;
totalChars = 0;
ifstream fin("MyData.txt");
string line;
if (!fin){
cout << "Error opening file : ";
return 0;
}
while (!fin.eof())
{
charCount = 0; // initialize character count for new line
getline(fin,line);
for (int i = 0 ; i<line.size(); i++)
{
cout.put(line[i]);
charCount++; // increment character count.
totalChars++; // increment total characters.
}
cout.put(NWLN); // marks end of current display line
lineCount++;
cout << "Number of characters in line " << lineCount
<< " is " << charCount << endl;
} // end outer while
cout << endl << endl << "Number of lines processed is " << lineCount << endl;
cout << "Total number of characters is " << totalChars << endl;
return 0;
}