Q:

C++ program to read a text file

0

C++ program to read a text file

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to read text character by character in C++

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
	char ch;
	const char *fileName="test.txt";
	
	//declare object
	ifstream file;
	
	//open file
	file.open(fileName,ios::in);
	if(!file)
	{
		cout<<"Error in opening file!!!"<<endl;
		return -1; //return from main
	}
	
	//read and print file content
	while (!file.eof()) 
	{
		file >> noskipws >> ch;	//reading from file
		cout << ch;	//printing
	}
	//close the file
	file.close();
	
	return 0;
}

Output

 
Hello friends, How are you?
I hope you are fine and learning well.
Thanks.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ program to write and read text in/from file... >>
<< C++ program to create a file...