concerning void*

Started by
6 comments, last by zorlack 20 years, 8 months ago
can someone tell me how to safely save a void* varieble to disk.
Advertisement
You can''t. Without any way to tell what the variable is pointing to, there''s no possible way to correctly serialize the data.

What, exactly, are you trying to do?

How appropriate. You fight like a cow.
i want to create a struct
struct data{      void * pData;      char namevar[10];};


on the program i decide what format i want to save on disk like int, char or float. then i want void to piont at it and save te struct on disk.
struct blarg{  void *ptr;  int size;}blarg b;FILE *f = fopen("blarg.dat", "wb");fwrite(b.ptr, b.size, 1, f); 


[edited by - Nypyren on August 11, 2003 10:43:00 PM]
Will it work if I say;
struct blarg{     void *ptr;     char name[10]; // name of the variable as you declare them on acces};char *name = "Hello";blarg b;FILE *f = fopen("blarg.dat", "wb");ptr = (char *)pname;fwrite(b.ptr, b.size, 1, f);
How about this:

struct blarg
{
void *ptr;
int size;
char name[10]; // name of the variable as you declare them on acces
};

char *name = "Hello";
blarg b;
FILE *f = fopen("blarg.dat", "wb";

b.ptr=pname;
char *data=(char *)b.ptr;

fwrite(data, b.size, 1, f);
I see what you are doing, but i want to save the structure not the new variable data that you entered
You still have to establish the struct''s real size when loading it.
It''s only convenient that the size is stored in, preferably the beginning of, the struct.
Reason for beginning of struct?
You can read it from file first, before loading the rest...

Niko Suni

This topic is closed to new replies.

Advertisement