記事ディレクトリ
序文
流れ説明。
流れの理解
ストリームの本質はオブジェクトです。
ストリームは、データとプログラムの間の中間デバイスです。
ストリームが存在するため、データを直接操作する必要はありませんが、ストリームを操作することで間接的にデータを操作できます。
ストリームアドバンテージ
統一された運用基準。
どのようなデータであっても、データがストリームに関連付けられている限り、データがどのように格納されているかを考慮する必要はなく、ストリームの運用基準に従って運用するだけで済みます。 . これは単一の運用基準です。
前述のように、ストリームの本質はオブジェクトであるため、ストリームには多くのメソッドが存在します.これらのメソッドは、データを操作するための統一されたインターフェイスを提供し、コードを変更せずに操作することもできます.さまざまなデータ.
C++ でのストリームの分類
C++ ミドル ストリームは、主に次の 3 つのカテゴリに分類されます。
- IO ストリーム: 入出力ストリーム、iostream
- ファイル ストリーム: ファイル操作の場合、fstream
- 文字列ストリーム: 主に文字列、stringstream に対する操作を実装します。
私流
[外部リンクの画像転送に失敗しました。ソース サイトには盗難防止リンク メカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします (img-pt9XOfB9-1681742287821) (C:\Users\hp\AppData\Roaming\Typora \typora-user-images\ image-20230417223559721.png)]
これら 3 つは本質的に同じであり、すべて抽象メソッドですが、操作の対象は異なります。これらのクラス間の関係図を以下に掲載します。
fstream自体はiostreamを継承しているため、なぜfstreamがcin/coutと同じように使えると言われているのかは上図からもわかります。
文字列ストリーム
基本的な考え方
入力および出力オブジェクトとしてメモリ内の文字列型変数を使用します。
特徴:
- 標準入出力ストリームと同様に、テキストとバイナリの間で変換できます。
- データを文字列⇔\Leftrightarrow⇔coutに保存 バイナリをASCIIに変換。
- 文字列 ⇔ \Leftrightarrow⇔cin ASCII からバイナリへのデータを取得します。
- 開閉する必要はありません。
- さまざまな種類のデータを保存できます。
使用
#include <sstream>
ostringstream ostr;//输出流
istringstream istr;//输入流
stringstream str;//输入/输出流
The sstring header file defines three types to support memory IO operations. これらの型は、文字列が IO ストリームであるように、文字列にデータを書き込み、文字列からデータを読み取ることができます。
istringstream は文字列からデータを読み取り、ostringstream は文字列にデータを書き込み、stringstream は文字列にデータを書き込み、文字列にデータを読み取ることができます。
#pragma once
#include<iostream>
#include<sstream>
#include<string>
class Stream {
public:
Stream() {}
Stream(std::string& str)
:str_(str)
{}
~Stream() {};
//输出流
void OsStringStream();
//输入流
void IsStringStream();
//输入输出流
void StringStringStream();
protected:
std::string str_;
};
#include"stream.h"
void Stream::OsStringStream() {
std::ostringstream oss(str_);
std::cout << oss.str() << std::endl;
}
void Stream::IsStringStream() {
std::string line;
std::istringstream iss(str_);
std::getline(iss, line);
std::cout << line << std::endl;
//创建输入流
std::istringstream str;
//拷贝字符串到输入流中
str.str("bye bye");
//用str_str返回str.str()的拷贝
std::string str_str = str.str();
std::cout << str_str << std::endl;
}
void Stream::StringStringStream() {
std::stringstream ss1(str_);
std::cout << ss1.str() << std::endl;
}
#include"stream.h"
int main() {
std::string str("ni hao");
Stream stream(str);
stream.IsStringStream();
stream.OsStringStream();
stream.StringStringStream();
return 0;
}
ファイル ストリーム
プログラムが実行されている場合、生成されたデータは一時的なデータであり、プログラムの実行が終了すると解放され、ファイルを通じてデータを永続化できます. C++ では、ファイル操作のためにヘッダー ファイル < fstream > を含める必要があります. テキストには次の 2 種類があります。
(1) テキストファイル: ファイルはテキストの ASCII コードの形式でコンピュータに保存されます。
(2) バイナリ ファイル: ファイルはコンピュータにバイナリ形式のテキストで保存され、通常、ユーザーはそれらを直接読み取ることはできません。
オペレーティング ファイルの 3 つのカテゴリ:
-
オブストリーム: 書き込み
-
ifstream: 読む
-
fstream: 読み書き
テキストファイル
ファイルを書き込む
次のように進めます。
- インクルード ヘッダー ファイル: #include<fstream>
- ストリーム オブジェクトを作成します。
- ファイルを開きます: ofs.open("file path", open method);
- 書き込みデータ: ofs<<"書き込みデータ";
- ファイルを閉じます: ofs.close();
方法を開きます:
注: ファイルを開く方法は | 演算子と組み合わせて使用できます。
void Csv::WriteCsvFile() {
//std::ofstream out_file("data.csv", std::ios::out); //std::ios::out 文件不存在则创建文件,有文件则清空文件
std::ofstream out_file;
out_file.open("data.csv", std::ios::out);
if (!out_file) {
std::cout << "open file failed" << std::endl;
return;
}
for(int i = 0; i < 5; ++i) {
out_file << 12 << ",";
out_file << 13 << ",";
out_file << 14 << std::endl;
}
out_file.close();
std::cout << "write file successed" << std::endl;
}
ファイルを読む
次のように進めます。
- インクルード ヘッダー ファイル: #include<fstream>
- ストリーム オブジェクトを作成します。
- ファイルを開き、ファイルが正常に開かれたかどうかを判断します。 ifs.open("file path", open method);
- データの読み取り: 4 つの読み取り方法
- ファイルを閉じます: ifs.close();
#pragma once
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
class Csv {
public:
Csv() {}
~Csv() {}
void ReadCsvFile();
void WriteCsvFile();
};
#include"csv.h"
void Csv::ReadCsvFile() {
//std::ifstream in_file("data.csv", std::ios::in);
std::ifstream in_file;
in_file.open("data.csv", std::ios::in);
if (!in_file) {
std::cout << "open fill failed" << std::endl;
}
int num = 0;
std::string line;
std::string file;
while (std::getline(in_file, line)) {
std::string file;
// 将整行字符串line读入到字符串流iss中
std::istringstream iss(line);
std::getline(iss, file, ',');
//将刚刚读取的字符串转换成int
std::cout << std::atoi(file.c_str()) << " ";
std::getline(iss, file, ',');
std::cout << std::atoi(file.c_str()) << " ";
std::getline(iss, file, ',');
std::cout << std::atoi(file.c_str()) << std::endl;
num++;
}
std::cout << "读取了" << num << "行" << std::endl;
if (5 == num) {
std::cout << "read file success" << std::endl;
}
in_file.close();
}
void Csv::WriteCsvFile() {
//std::ofstream out_file("data.csv", std::ios::out);
std::ofstream out_file;
out_file.open("data.csv", std::ios::out);
if (!out_file) {
std::cout << "open file failed" << std::endl;
return;
}
for(int i = 0; i < 5; ++i) {
out_file << 12 << ",";
out_file << 13 << ",";
out_file << 14 << std::endl;
}
out_file.close();
std::cout << "write file successed" << std::endl;
}
#include"csv.h"
int main() {
Csv csv;
csv.WriteCsvFile();
csv.ReadCsvFile();
return 0;
}
各ファイル ストリームにはデフォルトのファイル モードがあり、ifstream に関連付けられたファイルはデフォルトで in モードで開かれ、ofstream に関連付けられたファイルはデフォルトで out モードで開かれ、fstream に関連付けられたファイルはデフォルトで in および out モードで開かれます。
ofstream out; // 未指定文件打开模式
out.open("test.txt"); // 模式隐含设置为输出和截断
out.close(); // 关闭out
out.open("test2.txt",ofstream::app); //模式为输出和追加
out.close(); //关闭out