容器如何实现序列化?
#include <iostream>
#include <string>
using namespace std;
int main() {
// FILE *fd = fopen("test2.txt", "w+b");
// string s = "123";
// int z = sizeof(s);
// fwrite(&z, sizeof(z), 1, fd);
// fwrite(&s, z, 1, fd);
// fclose(fd);
FILE *fd;
string s;
int z;
fd = fopen("test2.txt", "r+b");
fread(&z, sizeof(z), 1, fd);
cout << z << endl;
fread(&s, z, 1, fd);
fclose(fd);
cout << s << endl;
return 0;
}
把注释去掉可以正常存储,加上注释后无法正确读出,请问如何解决?