最終更新日時(UTC):
が更新

履歴 編集

function
<fstream>

std::basic_ifstream::open

void open(
  const char* s,
  ios_base::openmode mode = ios_base::in); // (1)
void open(
  const filesystem::path::value_type* s,
  ios_base::openmode mode = ios_base::in); // (2) C++17
void open(
  const string& s,
  ios_base::openmode mode = ios_base::in); // (3)
void open(
  const filesystem::path& s,
  ios_base::openmode mode = ios_base::in); // (4) C++17

概要

ファイルを開く

効果

  • (1) : 仮引数sで指定したファイルを開く。
    • rdbuf()->open(s, mode | std::ios_base::in)を呼び出す(少なくとも読み取り操作ができる)。その結果が成功だった(戻り値がヌルポインタではなかった)場合、clear()を呼び出す。その結果が失敗だった(戻り値がヌルポインタだった)場合、setstate(failbit)を呼び出す。
  • (2) : std::filesystem::path::value_typeの型がcharではないときのみ定義される。効果は(1)と同じ。
  • (3) : ファイルを指定する引数の型がstd::stringである点を除き、(1)と同じ。
  • (4) : ファイルを指定する引数の型がstd::filesystem::pathである点を除き、(1)と同じ。

#include <iostream>
#include <fstream>

int main()
{
  std::ifstream s1;
  s1.open("file.txt");
  if (!s1) {
    std::cerr << "ファイルを開けませんでした。" << std::endl;
  }

  try
  {
    std::ifstream s2;
    s2.open("internal.dat", std::ios_base::in | std::ios_base::out | std::ios_base::binary);
    s2.exceptions(std::ios_base::failbit);
  } catch (const std::exception& e) {
    std::cerr << "ファイルを開けませんでした。" << std::endl;
  }
}

出力

ファイルを開けませんでした。
ファイルを開けませんでした。

バージョン

言語

  • C++98
  • C++17: std::filesystem::pathへの対応

参照