Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,930 questions

11,045 answers

573 users

How to check file status in C++

1 Answer

0 votes
#include <iostream>
#include <fstream>

using std::cout;
using std::endl;
using std::ifstream;

void check_file_status(ifstream &ifs)
{
	std::ios::iostate state;

	state = ifs.rdstate();

	if (state & std::ios::eofbit)
		cout << endl << "EOF" << endl;
	else if (state & std::ios::failbit)
		cout << endl << "Non-Fatal I/O error" << endl;
	else if (state &std::ios::badbit)
		cout << endl << "Fatal I/O error" << endl;
}

int main()
{
	ifstream ifs("d:\\data.txt");

	if (!ifs) {
		cout << "Error open file" << endl;
		return 1;
	}

	char ch;
	while (ifs.get(ch)) {
		cout << ch;
		check_file_status(ifs);
	}

	check_file_status(ifs);  
	ifs.close();

	return 0;
}


/*
run:

c c++ java
EOF

*/

 




answered Jul 2 by avibootz
...