From: dontmailme AT iname DOT com (Steamer) Newsgroups: comp.os.msdos.djgpp Subject: Re: newbie question:Writing a structure to a file Date: Fri, 23 Jun 2000 09:07:51 GMT Organization: always disorganized Lines: 68 Message-ID: <395328b5.4832069@news.freeserve.net> References: <3952C4BE DOT C9D570E9 AT cue DOT satnet DOT net> NNTP-Posting-Host: modem-232.missouri.dialup.pol.co.uk X-Trace: news6.svr.pol.co.uk 961751272 27580 62.137.76.232 (23 Jun 2000 09:07:52 GMT) NNTP-Posting-Date: 23 Jun 2000 09:07:52 GMT X-Complaints-To: abuse AT theplanet DOT net X-Newsreader: Forte Free Agent 1.11/32.235 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com logan AT cue DOT satnet DOT net wrote: > 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"); You need an exit or something here, otherwise it just goes on and writes anyway. > } > /* 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"); Similarly here. > } > fseek(data, SEEK_SET, 0); The fseek isn't needed, since 'data' already points to the start of the file. If you do use fseek then please check the documentation first - you have the arguments in the wrong order. > fread(&lineread,sizeof(lineread),1, data); > fclose(data); > return (0); > > Please help me to find out what am I doing wrong. You haven't given a complete program, and you haven't said what it does, and what you expected it to do, so it's a little difficult to be sure what you're doing wrong. However, you are writing a pointer to the file, which is unlikely to be useful, so I guess that this is your problem - presumably you wanted to write the string that the pointer points to. Note that it's usually a bad idea to fwrite a structure anyway, because the output will vary depending on the compiler used. What you need to do first is to design the format of the files that you want to generate. Then write your program so that it reads and writes files in that format, dealing with each structure member separately. S.