File tree Expand file tree Collapse file tree 1 file changed +36
-1
lines changed Expand file tree Collapse file tree 1 file changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ namespace std {
1616* basic_iostream[link /reference/istream/basic_iostream.md]
1717
1818## 概要
19-
19+ ファイルに対する入出力を提供する高水準な機構
2020
2121## メンバ関数
2222
@@ -49,3 +49,38 @@ namespace std {
4949| `off_type` | `Traits::off_type` | |
5050| `traits_type` | テンプレート仮引数`Traits` | |
5151
52+ ## 例
53+ ```cpp example
54+ #include <fstream>
55+ #include <ios>
56+ #include <iostream>
57+ #include <string>
58+
59+ int main() {
60+ std::fstream fs;
61+ fs.exceptions(std::ios::failbit | std::ios::badbit); // 例外を有効化する
62+ try {
63+ fs.open("./temp.txt", std::ios_base::out | std::ios_base::in | std::ios_base::trunc);
64+ if (!fs.is_open()) {
65+ throw std::runtime_error("Failed to open file"); // ファイルが開けなかった場合
66+ }
67+ int a = 1;
68+ long double b = 3.141592;
69+ std::string c = "hello";
70+ fs << a << " " << b << " " << c << std::endl;
71+ fs.seekg(0);
72+ int d;
73+ long double e;
74+ std::string f;
75+ fs >> d >> e >> f;
76+ if (!fs) {
77+ throw std::runtime_error("Failed to read from file"); // ファイルから読み取れなかった場合
78+ }
79+ std::cout << d << " " << e << " " << f << std::endl;
80+ } catch (const std::exception& e) {
81+ std::cerr << "Error: " << e.what() << std::endl;
82+ return 1;
83+ }
84+ return 0;
85+ }
86+ ```
You can’t perform that action at this time.
0 commit comments