C++ ファイル入出力ライブラリ - <fstream>
C++ において、 <fstream>は標準ライブラリ内でファイル入出力操作に使用されるクラスです。ファイルの読み書きに便利な方法を提供します。
fstreamであるiostreamライブラリの一部であり、テキストファイルとバイナリファイルの読み書きをサポートします。
fstreamクラスはiostreamライブラリ内のクラスで、継承元はistreamとostreamクラスであり、これは入力にも出力にも使用できることを意味します。
構文
fstreamの基本構文は次のとおりです:
#include <fstream>
int main() {
std::fstream file; // fstreamオブジェクトを作成
file.open("filename", mode); // ファイルを開く
// ファイル操作を実行
file.close(); // ファイルを閉じる
return 0;
}
int main() {
std::fstream file; // fstreamオブジェクトを作成
file.open("filename", mode); // ファイルを開く
// ファイル操作を実行
file.close(); // ファイルを閉じる
return 0;
}
ここでfilenameはファイル名です。modeはファイルを開くモードで、一般的なモードは次のとおりです:
std::ios::in:入力モードでファイルを開きます。std::ios::out:出力モードでファイルを開きます。std::ios::app:追記モードでファイルを開きます。std::ios::ate:ファイルを開いてファイルの末尾に移動します。std::ios::trunc:ファイルを開いてファイルを切り詰めます。つまりファイル内容を空にします。
例
テキストファイルへの書き込み:
例
#include <fstream>
#include <iostream>
int main() {
std::fstream file;
file.open("example.txt", std::ios::out); // 出力モードでファイルを開く
if (!file) {
std::cerr << "Unable to open file!" << std::endl;
return 1; // ファイルを開けなかった場合
}
file << "Hello, World!" << std::endl; // テキストを書き込む
file.close(); // ファイルを閉じる
return 0;
}
#include <iostream>
int main() {
std::fstream file;
file.open("example.txt", std::ios::out); // 出力モードでファイルを開く
if (!file) {
std::cerr << "Unable to open file!" << std::endl;
return 1; // ファイルを開けなかった場合
}
file << "Hello, World!" << std::endl; // テキストを書き込む
file.close(); // ファイルを閉じる
return 0;
}
現在のディレクトリに という名前のファイルを作成します。example.txtファイルの内容は次のとおりです:
Hello, World!
テキストファイルの読み取り
例
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::fstream file;
file.open("example.txt", std::ios::in); // 入力モードでファイルを開く
if (!file) {
std::cerr << "Unable to open file!" << std::endl;
return 1; // ファイルを開けなかった場合
}
std::string line;
while (getline(file, line)) { // 1行ずつ読み取る
std::cout << line << std::endl;
}
file.close(); // ファイルを閉じる
return 0;
}
#include <iostream>
#include <string>
int main() {
std::fstream file;
file.open("example.txt", std::ios::in); // 入力モードでファイルを開く
if (!file) {
std::cerr << "Unable to open file!" << std::endl;
return 1; // ファイルを開けなかった場合
}
std::string line;
while (getline(file, line)) { // 1行ずつ読み取る
std::cout << line << std::endl;
}
file.close(); // ファイルを閉じる
return 0;
}
もしexample.txtファイルに次の内容が含まれている場合:
Hello, World! This is a test file.
プログラムは次のように出力します:
Hello, World! This is a test file.
ファイルへの追記:
例
#include <fstream>
#include <iostream>
int main() {
std::fstream file;
file.open("example.txt", std::ios::app); // 追記モードでファイルを開く
if (!file) {
std::cerr << "Unable to open file!" << std::endl;
return 1; // ファイルを開けなかった場合
}
file << "Appending this line to the file." << std::endl; // テキストを追記
file.close(); // ファイルを閉じる
return 0;
}
#include <iostream>
int main() {
std::fstream file;
file.open("example.txt", std::ios::app); // 追記モードでファイルを開く
if (!file) {
std::cerr << "Unable to open file!" << std::endl;
return 1; // ファイルを開けなかった場合
}
file << "Appending this line to the file." << std::endl; // テキストを追記
file.close(); // ファイルを閉じる
return 0;
}
example.txtファイルの元の内容は次のとおりです:
Hello, World! This is a test file.
上記のプログラムを実行すると、ファイルの内容は次のようになります:
Hello, World! This is a test file. Appending this line to the file.その他の拡張