0

私はこのコードを持っています:

static std :: ifstream s_inF(argv[j]);
std :: cin.rdbuf(s_inF.rdbuf());

ファイルが正しく開かれ、問題がないことを確認するにはどうすればよいですか?

つまり、次のようなものを書きたいと思います。

static std :: ifstream s_inF(argv[j]);
std :: cin.rdbuf(s_inF.rdbuf());
if(.....)
{
  cout << "can not open the file" << endl;
  return 0;
}
...
.....
....
cin.close();

なにか提案を ?

4

2 に答える 2

2

のサブクラスであるすべてのオブジェクト(この場合は、std::basic_iosなど)s_inFには、ストリームがI/O操作の準備ができている場合にtrueを返すがあります。std::cinoperator bool

つまり、簡単に直接テストできます。例:

static std::ifstream s_inF(argv[j]);
std::cin.rdbuf(s_inF.rdbuf());
if (!s_inF)
{
  cout << "can not open the file" << endl;
  return 0;
}
// ...
cin.close();
于 2013-01-03T00:15:01.820 に答える
0

これに使用できますis_open。ここを参照してください:

http://www.cplusplus.com/reference/fstream/ifstream/is_open/

于 2013-01-02T23:57:05.093 に答える