delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/22/21:53:22

Message-ID: <3952C4BE.C9D570E9@cue.satnet.net>
Date: Thu, 22 Jun 2000 21:00:30 -0500
From: Ronald =?iso-8859-1?Q?Pati=F1o?= <logan AT cue DOT satnet DOT net>
X-Mailer: Mozilla 4.61 [en] (Win95; I)
X-Accept-Language: en
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: newbie question:Writing a structure to a file
Reply-To: djgpp AT delorie DOT com

Hi, can someone help me with this, can you please point out what my
mistakes are.

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;
};

this is the code I use to write data to the file :

struct db line;

  if (!(data=fopen("data.dat","wb")))
   {
      printf("Error opening file");
   }
 /*  strcpy(line.url,"http://www.ironmaiden.com");*/
   line.url="http://www.ironmaiden.com";
   line.test='T';
   fwrite(&line,sizeof(line),1, data);
   fclose(data);
   return (0);

and this is the code of the program I use to read the file

struct db lineread;

if (!(data=fopen("data.dat","rb")))
   {
      printf("Error opening file");
   }
   fseek(data, SEEK_SET, 0);
   fread(&lineread,sizeof(lineread),1, data);
   fclose(data);
  return (0);

Please help me to find out what am I doing wrong.

Thanks

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019