C++ ifstream Open File and report error
#include <fstream> #include <iostream> using namespace std; #include <process.h> int main(int argc, char* argv[] ) { if(argc != 2)//from www . j a v a2s. c o m { cerr << "\nFormat: oprint filename\n"; exit(-1); } char ch; //character to read ifstream infile; //create file for input infile.open( "data.txt" ); //open file if( !infile ) //check for errors { cerr << "\nCan't open " << "data.txt" << endl; exit(-1); } ofstream outfile; outfile.open("PRN"); //open it for printer while( infile.get(ch) != 0 ) //read a character outfile.put(ch); //write character to printer outfile.put('\x0C'); //formfeed to eject page return 0; }