C++ - How to write and read a structure that contain an object ? (to write and read binary)
问题 I'm trying to write a C structure in a file (to write in binary) and read it to recover it. I don't know if it is possible. Here is what I have : head.hh: #include <iostream> typedef struct s_test { char cmd[5]; std::string str; }t_test; main.cpp: #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include "head.hh" int main() { t_test test; int fd = open("test", O_APPEND | O_CREAT | O_TRUNC | O_WRONLY, 0666); test.cmd[0] = 's'; test.cmd[1] = 'm'; test.cmd[2]