This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
programming:cpp:binary [2022/04/11 05:34] odagawa created |
programming:cpp:binary [2022/04/30 17:48] (current) odagawa |
||
|---|---|---|---|
| Line 5: | Line 5: | ||
| ROOT を使っている限りは気にする必要がないが, emulsion とのデータをやり取りするために必要だった. | ROOT を使っている限りは気にする必要がないが, emulsion とのデータをやり取りするために必要だった. | ||
| - | <code cpp> | + | このとき,vector のような可変長配列は要素数を保持しておく必要がある.\\ |
| + | したがって,'' | ||
| + | 単なるメンバ変数とともに vector の要素数を追加で読み書きできるようにしておくことがポイントだと思う. | ||
| + | たとえば以下のようなクラスを考える. | ||
| + | |||
| + | <code cpp MyClass.hpp> | ||
| + | #ifndef MYCLASS_HPP | ||
| + | #define MYCLASS_HPP | ||
| + | |||
| + | #include < | ||
| + | |||
| + | class MyClass { | ||
| + | public : | ||
| + | int var1; | ||
| + | double var2; | ||
| + | std:: | ||
| + | }; | ||
| + | #endif | ||
| + | </ | ||
| + | |||
| + | 読み取りは以下のように行う. | ||
| + | |||
| + | <code cpp TestBinaryInput.cpp> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include " | ||
| + | |||
| + | bool ReadMyClassHeader(std:: | ||
| + | if (!ifs.read((char*)& | ||
| + | if (!ifs.read((char*)& | ||
| + | if (!ifs.read((char*)& | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | int main() { | ||
| + | std::string filename = " | ||
| + | std:: | ||
| + | std:: | ||
| + | MyClass myclass; | ||
| + | int vec_size = 0; | ||
| + | while ( ReadMyClassHeader(ifs, | ||
| + | myclass.vec.clear(); | ||
| + | myclass.vec.reserve(vec_size); | ||
| + | for ( int i = 0; i < vec_size; i++ ) { | ||
| + | double tmp; | ||
| + | ifs.read((char*)& | ||
| + | myclass.vec.push_back(tmp); | ||
| + | } | ||
| + | vec_myclass.push_back(myclass); | ||
| + | } | ||
| + | | ||
| + | for ( auto imyclass : vec_myclass ) { | ||
| + | // 適当な処理 | ||
| + | } | ||
| + | | ||
| + | std:: | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 書き込みは以下のように行う. | ||
| + | |||
| + | <code cpp TestBinaryOutput.cpp> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include " | ||
| + | |||
| + | void WriteMyClassHeader(std:: | ||
| + | int vec_size = myclass.vec.size(); | ||
| + | ofs.write((char*)& | ||
| + | ofs.write((char*)& | ||
| + | ofs.write((char*)& | ||
| + | } | ||
| + | |||
| + | int main() { | ||
| + | std::string filename = " | ||
| + | std:: | ||
| + | | ||
| + | std:: | ||
| + | // myclass vector を適当に作成 | ||
| + | | ||
| + | if ( !ofs ) { | ||
| + | std::cerr << "File : " << filename << " not found." | ||
| + | std:: | ||
| + | } | ||
| + | else if ( myclass_vector.empty() ) { | ||
| + | std::cerr << "Data null" << std::endl; | ||
| + | std:: | ||
| + | } | ||
| + | else { | ||
| + | for ( auto myclass : myclass_vector ) { | ||
| + | WriteMyClassHeader(ofs, | ||
| + | for ( auto ivec : myclass.vec ) { | ||
| + | ofs.write((char*)& | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | std:: | ||
| + | | ||
| + | } | ||
| </ | </ | ||