如何将对象的指针写入文件
How to write pointers of an object into a file?
我有一种情况,我需要将对象的指针存储到一个文件中,并在同一进程中再次读取它。我该怎么做呢?
现在我这样写/读:
Myclass* class = <valid pointer to Myclass>
FILE* output_file = fopen(filename, "w");
fwrite(class, sizeof(class), 1, output_file)
// and read it
FILE* in_file = fopen(filename, "r");
Myclass* class_read
fread(class_read, sizeof(class_read), 1, in_file)
回读时没有看到正确的值。我将在相同的地址空间中读写这些文件
要读写指针本身,需要传递它的地址,而不是它指向的地址:
fwrite(&class, sizeof(class), 1, output_file);
fread (&class_read, sizeof(class_read), 1, in_file);
^
您正在编写class
指向的前几个字节,然后尝试将它们读回class_read
指向的任何内容(如果它还不是有效指针,则失败)。
相关文章:
- 为什么当我尝试将char输入到int时,文件指针会卡住
- fprintf 不打印到文件,当文件指针在其他地方声明时
- 如何从NTFS中的$ MFT文件访问文件的每个块的文件指针
- 尽管文件指针正确,但在fclose()期间的segfault
- 当调用FREAD()函数时,文件指针如何移动
- C++如何复制文件 * 指针
- 如何将文件描述符 (int fd) 转换为文件指针 ( 文件* fp )
- Visual Studio 2010 调用 _locking() 失败 (errno = 22) 当文件指针高于 (2^
- 如何在 c++ 中查找文件指针位置
- c++文件指针工作不正常
- 做一个popen(),将文件*指针放入fstream中,pclose()呢?
- 在Linux中,多个进程使用文件指针读取文件时使用的共享虚拟内存
- 无法声明文件指针数组
- 将文件指针重定向到C/C++中的字符串
- 如何检查文件指针/描述符/句柄是否关联到同一个文件/管道/终端
- 在记录中定位文件指针
- 流和文件指针移动
- 如何将文件指针移回一个整数
- C++ 如何使用文件指针以增量方式写入文件
- 为什么std::istream::read不根据读取的字节移动文件指针