7、C++Primer 4 thノート、標準IOライブラリ(2)
5476 ワード
1、ファイルの入出力
履歴上、IO標準ライブラリはC++stringsタイプの文字列ではなくCスタイルの文字列をファイル名として使用します.
fstreamオブジェクトを別の異なるファイルに関連付ける場合は、現在のファイルを閉じてから別のファイルを開く必要があります.
2、ファイル中のファイルフローの状態を読む
プログラマが複数のファイルをファイルストリームで読み書きする必要がある場合は、別のファイルを読む前にclearを呼び出してストリームのステータスをクリアする必要があります.
サンプルプログラム
3、ファイルモード
各fstreamクラスには、フローが開く異なるモードを指定するための異なるモードのセットが定義されています.条件ステータスフラグと同様に、ファイルモードも整形定数であり、指定されたファイルを開くときにビットオペレータを使用して複数のモードを設定できます.
表示オープンモード
in
ファイルを開く
out
ファイルを開いて書き込みを行う
app
書くたびにファイルの末尾を見つけます
ate
ファイルを開くとすぐにファイルの最後に配置
trunc
ファイルを開くときに既存のファイルストリームをクリア
binary
バイナリモードでIO動作
out、trunc、appモードはofstreamオブジェクトまたはfstreamオブジェクトへの関連付けを指定するためにのみ使用できます.
のファイル;Inモードはifstreamまたはfstreamオブジェクトに関連付けられたファイルのみを指定できます.すべて
のファイルはateモードまたはbinaryモードで開くことができます.ateモードは開く時のみ有効:ファイル打
ファイルの最後に配置されます.binaryモードで開いているストリームは、ファイルをバイトシーケンスとして開きます.
ストリーム内の文字を説明するのではなく、理.
outモードで開いているファイルは空になります.ファイルが同時にin,outで開かれると空になりません.
モードは、ファイルのプロパティがストリームのプロパティではありません.
オープンモードの有効な組み合わせを示します
out
ファイルを開いて書き込みを行い、ファイル内の既存のデータを削除します.
out | app
ファイルを開いて書き込みを行い、ファイルの最後に書き込みます.
out | trunc
outモードと同じ
in
ファイルを開く
in | out
ファイルを開いて読み取り、書き込みを行い、ファイルの先頭に配置します.
in | out | trunc
ファイルを開いて読み取り、書き込みを行い、ファイル内の既存のデータを削除します.
4、文字ストリーム
文字ストリームを表すアクション
stringstream strm;
フリーstringstreamオブジェクトの作成
stringstream strm(s);
sのコピーを格納するstringstreamオブジェクトを作成します.sはstringタイプのオブジェクトです.
strm.str()
strmに格納されているstringタイプオブジェクトを返します.
strm.str(s)
stringタイプのsをstrmにコピーしvoidを返します
文字ストリームの古典的な操作例
1)stringstreamが提供する変換とフォーマット
stringstreamオブジェクトの一般的な使い方の1つは、複数のデータ型間で自動フォーマットを実現する際にこのクラスタイプを使用する必要があることです.sstream入力と出力操作は自動的に算術タイプを対応する
stringは形式を表しますが、逆にしてもいいです.
典型的な例
input_を読み込むためにstringは、stringオブジェクトをいくつかの部分に分解し、必要なデータの周囲にあるラベルを読み取り、無視しなければならない.入力オペレータはタイプの値を読み込むため、読み込まれたオブジェクトタイプはstringstreamによって読み込まれた値のタイプと一致する必要があります.一般的に、入力オペレータを使用してstringを読むと、空白は無視されます.
参照先:
[1] http://blog.163.com/zhoumhan_0351/blog/static/39954227201002945157577/
[2] http://blog.163.com/zhoumhan_0351/blog/static/39954227201003005237697/
[3] http://blog.163.com/zhoumhan_0351/blog/static/399542272010030103055465/
[4] http://blog.163.com/zhoumhan_0351/blog/static/39954227201002192139554/
[5] http://blog.163.com/zhoumhan_0351/blog/static/3995422720103171303182/
[6]入出力ストリームを深く入り込む
http://blog.163.com/zhoumhan_0351/blog/static/399542272010495432659/
http://blog.163.com/zhoumhan_0351/blog/static/39954227201032392545410/
履歴上、IO標準ライブラリはC++stringsタイプの文字列ではなくCスタイルの文字列をファイル名として使用します.
fstreamオブジェクトを別の異なるファイルに関連付ける場合は、現在のファイルを閉じてから別のファイルを開く必要があります.
2、ファイル中のファイルフローの状態を読む
プログラマが複数のファイルをファイルストリームで読み書きする必要がある場合は、別のファイルを読む前にclearを呼び出してストリームのステータスをクリアする必要があります.
サンプルプログラム
// for each file in the vector
while (it != files.end()) {
ifstream input(it->c_str()); // open the file;
// if the file is ok, read and "process" the input
if (!input)
break; // error: bail out!
while(input >> s) // do the work on this file
process(s);
++it; // increment iterator to get
next file
}
//check out the difference between these two programs.
ifstream input;
vector<string>::const_iterator it = files.begin();
// for each file in the vector
while (it != files.end()) {
input.open(it->c_str()); // open the file
// if the file is ok, read and "process" the input
if (!input)
break; // error: bail out!
while(input >> s) // do the work on this file
process(s);
input.close(); // close file when we're done with it
input.clear(); // reset state to ok
++it; // increment iterator to get next file
}
3、ファイルモード
各fstreamクラスには、フローが開く異なるモードを指定するための異なるモードのセットが定義されています.条件ステータスフラグと同様に、ファイルモードも整形定数であり、指定されたファイルを開くときにビットオペレータを使用して複数のモードを設定できます.
表示オープンモード
in
ファイルを開く
out
ファイルを開いて書き込みを行う
app
書くたびにファイルの末尾を見つけます
ate
ファイルを開くとすぐにファイルの最後に配置
trunc
ファイルを開くときに既存のファイルストリームをクリア
binary
バイナリモードでIO動作
out、trunc、appモードはofstreamオブジェクトまたはfstreamオブジェクトへの関連付けを指定するためにのみ使用できます.
のファイル;Inモードはifstreamまたはfstreamオブジェクトに関連付けられたファイルのみを指定できます.すべて
のファイルはateモードまたはbinaryモードで開くことができます.ateモードは開く時のみ有効:ファイル打
ファイルの最後に配置されます.binaryモードで開いているストリームは、ファイルをバイトシーケンスとして開きます.
ストリーム内の文字を説明するのではなく、理.
outモードで開いているファイルは空になります.ファイルが同時にin,outで開かれると空になりません.
モードは、ファイルのプロパティがストリームのプロパティではありません.
オープンモードの有効な組み合わせを示します
out
ファイルを開いて書き込みを行い、ファイル内の既存のデータを削除します.
out | app
ファイルを開いて書き込みを行い、ファイルの最後に書き込みます.
out | trunc
outモードと同じ
in
ファイルを開く
in | out
ファイルを開いて読み取り、書き込みを行い、ファイルの先頭に配置します.
in | out | trunc
ファイルを開いて読み取り、書き込みを行い、ファイル内の既存のデータを削除します.
4、文字ストリーム
文字ストリームを表すアクション
stringstream strm;
フリーstringstreamオブジェクトの作成
stringstream strm(s);
sのコピーを格納するstringstreamオブジェクトを作成します.sはstringタイプのオブジェクトです.
strm.str()
strmに格納されているstringタイプオブジェクトを返します.
strm.str(s)
stringタイプのsをstrmにコピーしvoidを返します
文字ストリームの古典的な操作例
string line, word; // will hold a line and word from input,respectively
while (getline(cin, line))
{ // read a line from theinput into line
// do per-line processing
istringstream stream(line); // bind to stream to the line we read
while (stream >> word)
{ // read a word from line
// do per-word processing
}
}
1)stringstreamが提供する変換とフォーマット
stringstreamオブジェクトの一般的な使い方の1つは、複数のデータ型間で自動フォーマットを実現する際にこのクラスタイプを使用する必要があることです.sstream入力と出力操作は自動的に算術タイプを対応する
stringは形式を表しますが、逆にしてもいいです.
典型的な例
int val1 = 512, val2 = 1024;
ostringstream format_message;
// ok: converts values to a string representation
format_message << "val1: " << val1 << "
"
<< "val2: " << val2 << "
";
//the following is how to read
//str member obtains the string associated with a stringstream
istringstream input_istring(format_message.str());
string dump; // place to dump the labels from the formatted message
// extracts the stored ascii values, converting back to arithmetic types
input_istring >> dump >> val1 >> dump >> val2;
cout << val1 << " " << val2 << endl; // prints 512 1024
input_を読み込むためにstringは、stringオブジェクトをいくつかの部分に分解し、必要なデータの周囲にあるラベルを読み取り、無視しなければならない.入力オペレータはタイプの値を読み込むため、読み込まれたオブジェクトタイプはstringstreamによって読み込まれた値のタイプと一致する必要があります.一般的に、入力オペレータを使用してstringを読むと、空白は無視されます.
参照先:
[1] http://blog.163.com/zhoumhan_0351/blog/static/39954227201002945157577/
[2] http://blog.163.com/zhoumhan_0351/blog/static/39954227201003005237697/
[3] http://blog.163.com/zhoumhan_0351/blog/static/399542272010030103055465/
[4] http://blog.163.com/zhoumhan_0351/blog/static/39954227201002192139554/
[5] http://blog.163.com/zhoumhan_0351/blog/static/3995422720103171303182/
[6]入出力ストリームを深く入り込む
http://blog.163.com/zhoumhan_0351/blog/static/399542272010495432659/
http://blog.163.com/zhoumhan_0351/blog/static/39954227201032392545410/