open file
ifstream in;
in.open("input.txt"); //parameter is uchar*! just correspond with the main parameter, argv[1].
ifstream& openFile(ifstream& inf, const char* pN) { inf.close(); //close flow inf.clear(); //clear any existing error //combine inf and file, open file. inf.open( pN ); return inf; } ifstream& openFile(ifstream& inf, const string& pN) { inf.close(); //close flow inf.clear(); //clear any existing error //combine inf and file, open file. inf.open( pN.c_str()); //!!must convers string into uchar* return inf; }
如果文件打开成功,则上述函数返回值(inf)有效。 同cin
if(!openFile( ......) )
//文件打开没有成功, 终止允许。
————————————————————————————————————
2 cin
cin.clear(); //使得输入流有效 cin.sync(); //清空输入流缓冲中无效的数据 // it is a must