Mail Archives: djgpp/2000/06/23/06:35:29
Ronald Patiño <logan AT cue DOT satnet DOT net>:
> Im trying to write this structure to a file, and then
> retrive it from
> another program,
> but i'm having troubles.
>
> struct db{
> char *url;
> char test;
> };
Your struct contains a pointer to a string...
> this is the code I use to write data to the file :
>
> struct db line;
[...]
> line.url="http://www.ironmaiden.com";
> line.test='T';
> fwrite(&line,sizeof(line),1, data);
Here you are writing the value of the pointer (ie: a
memory address) and not the value of the string it
points to.
When you read it from another program, your pointer
will contain some address which does not refer to the
same string, and most probably will crash accessing an
invalid pointer.
You should try something like this :
/* warning: untested code */
struct db{
char url[ MAX_URL_LEN ];
char test;
};
...
strcpy(line.url, "http://www.ironmaiden.com");
line.test='T';
fwrite(&line,sizeof(line),1, data);
ciao
Giacomo
-----------------------------------------------------
Giacomo Degli Esposti - pad2369 AT iperbole DOT bologna DOT it
- Raw text -