GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::basic_fstream - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. fstream
  3. basic_fstream

最終更新日時: 2026年01月08日 20時45分05秒 (JST)
yoh が更新

履歴 編集

class template
<fstream>

std::basic_fstream

namespace std {
  template <class CharT, class Traits = char_traits<CharT>>
  class basic_fstream : public basic_iostream<CharT, Traits>;

  using fstream  = basic_fstream<char>;
  using wfstream = basic_fstream<wchar_t>;
}

概要

ファイルに対する入出力を提供する高水準な機構

メンバ関数

名前 説明 対応バージョン
(constructor) コンストラクタ
(destructor) デストラクタ
operator= ムーブ代入 C++11
swap 値の交換 C++11
rdbuf ストリームバッファオブジェクトの取得
is_open ファイルを開いているかの判定
open ファイルを開く
close ファイルを閉じる

ネイティブハンドルの取得

名前 説明 対応バージョン
native_handle() ネイティブハンドルを取得する[処理系定義] C++26

非メンバ関数

名前 説明 対応バージョン
swap 2つのオブジェクトを入れ替える C++11

メンバ型

名前 説明 対応バージョン
char_type テンプレート仮引数CharT
int_type Traits::int_type
pos_type Traits::pos_type
off_type Traits::off_type
traits_type テンプレート仮引数Traits
native_handle_type 処理系定義のネイティブハンドルの型typename basic_filebuf<CharT, Traits>::native_handle_type C++26

例

#include <fstream>
#include <ios>
#include <iostream>
#include <string>

int main() {
  std::fstream fs;
  fs.exceptions(std::ios::failbit | std::ios::badbit); // 例外を有効化する
  try {
    fs.open("./temp.txt", std::ios_base::out | std::ios_base::in | std::ios_base::trunc);
    if (!fs.is_open()) {
      throw std::runtime_error("Failed to open file"); // ファイルが開けなかった場合
    }
    int a = 1;
    long double b = 3.141592;
    std::string c = "hello";
    fs << a << " " << b << " " << c << std::endl;
    fs.seekg(0);
    int d;
    long double e;
    std::string f;
    fs >> d >> e >> f;
    if (!fs) {
      throw std::runtime_error("Failed to read from file"); // ファイルから読み取れなかった場合
    }
    std::cout << d << " " << e << " " << f << std::endl;
  } catch (const std::exception& e) {
    std::cerr << "Error: " << e.what() << std::endl;
    return 1;
  }
  return 0;
}

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.