This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
programming:cpp:binary [2022/04/11 07:22] odagawa |
programming:cpp:binary [2022/04/30 17:48] (current) odagawa |
||
---|---|---|---|
Line 5: | Line 5: | ||
ROOT を使っている限りは気にする必要がないが, emulsion とのデータをやり取りするために必要だった. | ROOT を使っている限りは気にする必要がないが, emulsion とのデータをやり取りするために必要だった. | ||
- | たとえば以下のようなクラスをかんがえる. | + | このとき,vector |
+ | したがって,'' | ||
+ | 単なるメンバ変数とともに vector の要素数を追加で読み書きできるようにしておくことがポイントだと思う. | ||
- | <code cpp> | + | たとえば以下のようなクラスを考える. |
- | class Myclass | + | |
+ | <code cpp MyClass.hpp> | ||
+ | #ifndef MYCLASS_HPP | ||
+ | #define MYCLASS_HPP | ||
+ | |||
+ | #include < | ||
+ | |||
+ | class MyClass | ||
public : | public : | ||
int var1; | int var1; | ||
Line 14: | Line 23: | ||
std:: | std:: | ||
}; | }; | ||
+ | #endif | ||
</ | </ | ||
読み取りは以下のように行う. | 読み取りは以下のように行う. | ||
- | <code cpp> | + | < |
#include < | #include < | ||
+ | #include < | ||
#include < | #include < | ||
#include < | #include < | ||
Line 60: | Line 71: | ||
書き込みは以下のように行う. | 書き込みは以下のように行う. | ||
- | <code cpp> | + | < |
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include <iomanip> | ||
+ | #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:: | ||
+ | | ||
+ | } | ||
</ | </ |