C 資料 電脳梁山泊烏賊塾 ファイルの入出力 C++ のバイナリファイル入出力 初めに 此処では Visual Studio 2017 を起動し 新しいプロジェクトで Visual C++ の Windows デスクトップを選択し Windows コンソールアプリケーションを作成する

Similar documents
ファイル入出力

1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >>

情報処理演習 B8クラス

ファイル操作-バイナリファイル

VB6互換のファイルの処理

Taro-ファイル処理(公開版).jtd

プログラミング演習 土曜日(Q組)

PowerPoint プレゼンテーション

構造体

untitled

VB実用Ⅲ⑩ フリーデータベースⅡ

C言語講座 ~ファイル入出力編~

Microsoft Word - Cプログラミング演習(10)

PowerPoint Presentation

Microsoft Word - no15.docx

データベースプログラミング

ICONファイルフォーマット

Userコントロール

64bit環境で32bitコンポーネントの利用

ファイル操作

Łñ“’‘‚2004

プリント


画像ファイルを扱う これまでに学んだ条件分岐, 繰り返し, 配列, ファイル入出力を使って, 画像を扱うプログラムにチャレンジしてみよう

シーケンシャルファイルの操作

memo

プログラミング基礎

programmingII2019-v01

Microsoft Word - Cプログラミング演習(12)

Microsoft Word - no205.docx

相性占いプログラム

Microsoft PowerPoint - kougi2.ppt

新版 明解C++入門編

解きながら学ぶC++入門編

Microsoft PowerPoint - prog04.ppt

ユーザーズマニュアル

Java講座

シングルドキュメントの作成 新しいプロジェクトで MFC アプリケーションを選択すると アプリケーションの種類のオプションのダイアログが開くので アプリケーションの種類にシングルドキュメントを選択して 次へボタンをクリックする ( 此処で完了ボタンをクリックしても構わないが 不要なフレームペインを取

コンピュータ概論

第 1 章 : はじめに RogueWave Visualization for C++ の Views5.7 に付属している Views Studio を使い 簡単な GUI アプリケーションの開発手順を紹介します この文書では Windows 8 x64 上で Visual Studio2010

Microsoft PowerPoint - kougi4.ppt

実習を行う上での心構えについて

NotifyIconコントロール

プロセス間通信

PowerPoint プレゼンテーション

データアダプタ概要

オブジェクト指向プログラミング・同演習 5月21日演習課題

Microsoft PowerPoint - prog06.ppt

1 1 Arduino とは Arduino アルドゥイーノ は ワンボードマイコンの一種で オープンソースハードウェアであ り 組み立て済みの基板を購入することもできるほか 誰でも自分の手で Arduino を組み立てる ことができます USBコネクタでPCと接続して利用します デジタルポートとア

Microsoft Word - C.....u.K...doc

JavaプログラミングⅠ

PowerPoint Presentation

EnSight 10.1の新機能

Visual Basic 資料 電脳梁山泊烏賊塾 コレクション初期化子 コレクション初期化子 初めに.NET 版の Visual Basic では 其れ迄の Visual Basic 6.0 とは異なり 下記の例の様に変数宣言の構文に 初期値を代入する式が書ける様に成った 其の際 1 の様に単一の値

PowerPoint プレゼンテーション

プログラミング基礎

JavaプログラミングⅠ

Transcription:

ファイルの入出力 C++ のバイナリファイル入出力 初めに 此処では Visual Studio 2017 を起動し 新しいプロジェクトで Visual C++ の Windows デスクトップを選択し Windows コンソールアプリケーションを作成する 使用クラス C++ の場合 ファイルの入出力に使用するクラスは ifstream ofstream fstream の 3 種類が有り 頭に i(input) が付いた ifstream が入力 o(output) が付いた ofstream が出力 何も付かない fstream が両用を表す クラス名 入出力別 派生元クラス ifstream 入力 istream ofstream 出力 ostream fstream 両用 iostream 孰れも fstream をインクルードする事で使える様に成る -1-

オープンモード オープンモードは 列挙型 open_mode の各ビットの論理和 (OR) を取って設定する open_mode は ios クラスの公開部で下記の様に定義されて居る 入出力別 モード 値 解説 両用 ios::binary 0 ファイルをバイナリモードで開く 入力 ios::in 1 読み込み専用で開く ios::ate 4 開く時に EOF 迄移動する ios::out 2 出力用にファイルを開く 出力 ios::app 8 追加 ( アペンド ) 出力 ios::trunc 16 既存のファイルを上書きする 出力モードでバイナリファイルをオープンする時は 下記の孰れかを記述する std::ofstream tofile ("sample.bin", std::ios::binary); std::fstream inoutfile ("sample.bin", std::ios::binary std::ios::out); 入力モードでバイナリファイルをオープンする時は 下記の孰れかを記述する std::ifstream fromfile ("sample.bin", std::ios::binary); std::fstream inoutfile ("sample.bin", std::ios::binary std::ios::in); 入出力両用でファイルをオープンする時は ios::in と ios::out の和をモードに指定する std::fstream inoutfile ("sample.bin", std::ios::binary std::ios::in ios::out); ファイルのオープンとクローズ 下記の様に コンストラクタにファイル名を指定した場合 ファイルストリームのインスタンスが生成されると同時に ファイルがオープンされる std::ifstream fromfile("sample.bin", std::ios::binary); if (!fromfile) std::cout << "Unable to open for input mode! n"; else std::cout << "Successfully open for input mode! n"; std::ofstream tofile("sample.bin", std::ios::binary); if (!tofile) std::cout << "Unable to open for output mode! n"; else std::cout << "Successfully open for output mode! n"; std::fstream inoutfile("sample.bin", std::ios::binary std::ios::in std::ios::out); if (!inoutfile) std::cout << "Unable to open for in/out mode! n"; else std::cout << "Successfully open in/out mode! n"; inoutfile.close(); 亦 下記の様に ファイルを指定せずにファイルストリームの宣言丈を行い 後にファイルを明示的にオープンする事も出来る fromfile.open("sample.bin", std::ios::binary); -2-

std::ofstream tofile; tofile.open("sample.bin", std::ios::binary); std::ofstream inoutfile; inoutfile.open("sample.txt", std::ios::binary std::ios::out std::ios::out); inoutfile.close(); コンストラクタにファイル名を指定してオープンしたり open 関数を使用してオープンしたファイルは ファイルストリームが破棄された時点 (delete するか スコープ外に出る時点 ) で自動的にクローズされるが オープンしたファイルは 必ず close でクローズする物だと憶えて欲しい 入力 バイナリファイルからデータを読み込む時 下記のモードでファイルをオープンする (ios::in は ifstream では不要 ) モード ios::in ios::binary 解説読み込み専用で開くファイルをバイナリモードで開く 読み取り専用モードで開くには 下記の様に記述する std::ifstream fromfile("sample.bin", std::ios::binary); fromfile.open("sample.bin", std::ios::binary); std::fstream inoutfile ("sample.bin", std::ios::binary std::ios::in); std::fstream inoutfile; inoutfile.open("sample.bin", std::ios::binary std::ios::in); 下記に バイナリファイル sample1.bin より int 型のデータを 2 個連続で読み込むコードを示す int dat; fromfile.open("sample1.bin", std::ios::binary); if (!fromfile) std::cout << "Unable to open for input mode! n"; else std::cout << "Successfully open for input mode! n"; fromfile.read((char*)&dat, sizeof(int)); std::cout << dat << std::endl; fromfile.read((char*)&dat, sizeof(int)); std::cout << dat << std::endl; バイナリファイル sample1.bin はデバッグ時には ソースファイル (.cpp) と同じディレクトリに格納されて居る物とする -3-

猶 総てのデータを読み込むには ファイルの終端に達すると true に成る eof 関数 (End of file) を利用する fromfile.open("sample.bin", std::ios::binary); while (!fromfile.eof( )) fromfile.read((char*)&dat, sizeof(int)); std::cout << dat << std::endl; fromfile.close( ); 下記に バイナリファイル sample2.bin より 構造体 Person 型のデータを読み込むコードを示す ( 先に出力のサンプルコードで sample2.bin を作成して置くと結果が確認出来る ) struct Person char name[20]; char sex; int age; double height; double weight; ; Person p; バイナリファイル sample2.bin はデバッグ時には ソースファイル (.cpp) と同じディレクトリに格納されて居る物とする fromfile.open("sample2.bin", std::ios::binary); fromfile.read((char*)&p, sizeof(p)); printf("%s %c %d %f %f n n", p.name, p.sex, p.age, p.height, p.weight); 出力 バイナリファイルにデータを書き込む時 下記のモードでファイルをオープンする (ios::out は ofstream では不要 ) モード ios::out ios::binary 意味出力用にファイルを開くファイルをバイナリモードで開く 下記に バイナリファイル sample1.bin に int 型のデータを 2 個連続で書き込むコードを示す ( 上書きモードなので元のデータは消去される ) -4-

int dat; std::ofstream tofile; tofile.open("sample1.bin", std::ios::binary); if (!tofile) std::cout << "Unable to open for output mode! n"; else std::cout << "Successfully open for output mode! n"; tofile.write((char*)&dat, sizeof(int)); dat -= 1; tofile.write((char*)&dat, sizeof(int)); std::cout << "Saved!" << std::endl; 下記に バイナリファイル sample2.bin に 構造体 Person 型のデータを書き込むコードを示す ( 上書きモードなので元のデータは消去される ) struct Person char name[20]; char sex; int age; double height; double weight; ; char w[20] = " 烏賊太郎 "; Person p; strcpy_s(p.name, w); p.sex = 'M'; p.age = 68; p.height = 169.8; p.weight = 64.7; printf("%s %c %d %f %f n n", p.name, p.sex, p.age, p.height, p.weight); std::ofstream tofile; tofile.open("sample2.bin", std::ios::binary); tofile.write((char*)&p, sizeof(p)); std::cout << "Saved!" << std::endl; -5-

上記の結果 下図の様なバイナリファイルが生成される 構造体のパディング ( アライメント ) や各データ型の格納法が良く解る -6-