Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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:

Task 0: Download and run countChars.cpp and copyFile.cpp, try to understand the code. MyData.txt (rather than from the keyboard). Also display the total number of characters and total lines in the file including the newline characters. Remember you do not need to write in an another external file (copy file), simply display total number of characters and total lines from the MyData.txt file. What and how to submit: When you are done and testing is successful, show your work to your instructor. If your instructor says OK, upload your cpp file and screenshot of your sample Input and output

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;
}