Answer to Question #224230 in C++ for Shank

Question #224230
Write a program to read the contents of a text file and count the number of characters read from the file.
1
Expert's answer
2021-08-09T11:06:41-0400
#include <iostream>
#include <fstream>//using standart c++ lib
#include <string.h>
using namespace std;
int main()
{
	string fname;//file name
	cout << "Please enter file name and type(.txt): ";//for example "input.txt"
	cin >> fname;
	ifstream file(fname);
	if (!file)
		return cout << "File not found!!!\n", 0;
	int numChars = 0;//number characters  readed file
	while (!file.eof())
	{
		char ch;
		file >> ch;
		numChars++;
	}
	cout << "number of characters read from the file: " << numChars << endl;
	file.close();//close file procces
	return  0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS