io20_6.05.c

来自「C++ Primer(第三版)的随书源代码」· C语言 代码 · 共 43 行

C
43
字号
// #include <fstream>
#include <fstream.h>
#include <string>

/**
 ** requires that these files exist in the directory
 ** this program is executed within
 **
 stanl@john:d.ch20 249 : a.out

 ok: file open: Melville
 ok: file open: Joyce
 ok: file open: Musil
 ok: file open: Proust
 ok: file open: Kafka
 **
 **/


const int fileCnt = 5;
string fileTabl[ fileCnt ] = {
    "Melville", "Joyce", "Musil", "Proust", "Kafka"
};

int main()
{
    ifstream inFile; // not attached to any file

    for ( int ix = 0; ix < fileCnt; ++ix )
    {
          inFile.open( fileTabl[ix].c_str() );
	  if ( ! inFile ) 
	       cerr << "oops! unable to open file: "
		    << fileTabl[ix] << endl;
	  else cerr << "ok: file open: "
		    << fileTabl[ix] << endl;

          // ... process file

          inFile.close();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?