c - binary files writing/reading problems -


OK I have a problem with my code to read the binary file ...

Firstly I'll give you Showing my writing code:

  Saving Zero Book (char * file_name, struct book * current) {FILE * out; Book Buff; Out = FOPN (file_name, "wb"); If (outside! = Null) {printf_s ("writing to file ..."); Do {if (current! = NULL) {strcpy (buf.catalog_number, current-> catalog_number); Strcpy (buf.author, current-> author); Buf.price = Current-gt; value; Strcpy (buf.publisher, current-> publisher); Strcpy (buf.title, current-> title); Buf.price = current-> Year_printed; Fillit (& amp; buf, sizeof (book), 1, outside); } Current = current->; next; } While (current! = Null); Printf_s ("done \ N!"); Fclose (outside); }}  

and my "version" to read here:

  int book_open (struct book * current, char * file_name) {FILE * in ; Book Buff; Book * vnext; integer number; Int i; In = FOPN ("west", "rb"); Printf_s ("Reading database from% s ...", file_name); If (! In) {printf_s ("\ nERROR!"); Return 1; } I = Fred (& amp; buf, sizeof (book), 1, in); While (! FIF (in)) {if (current! = NULL) {current = malloc (sizeof (book)); Current-gt; Next = null; } Strcpy (current-> catalog_number, buf.catalog_number); Strcpy (current-> title, buf.title); Strapepy (current-gt; publisher, buf.publisher); Current-gt; Value = buf.price; Current-> Year_promised = buf.year_published; Fred (& amp; buf, 1, in sizeof (book)); While (current-> next! = Null) current = current->; Fclose (in); } Printf_s ("Done!"); Return 0; }  

I need to be able to save my linked list to the binary file and read it back ... Please help me program does not read the circumstances only every time Or its accident ...

  1. your do..while < / Code> Loop formation can be better if you are going to check at the end, do not check in the beginning if you think that you have to do this, you might be right For example, here you are going to call (current = blue) {}

  2. from for example, What are you trying to do (current! = Null) {} ? You are setting the current node in a new book in your loop, and its next element is creating the NULL . Why? Why do not you mirror the loop in the method of writing?

  3. See what you are doing if current == NULL inter alia - you strakpy in your reading method Ing Do not do this.

  4. You think fclose (in) within within loop Book_open .

I will compile it once.


OK, I've edited the code to a certain extent 2 assumptions

  1. This is not the problem of homework
  2. The book is only 1 indicator ( next) and everything else, otherwise this is an array with allocated memory for it

Save the book - Just loops and writes

  FILE * out; Book Buff; Out = FOPN (file_name, "wb"); If (exit == faucet) return; Printf_s ("Writing in file ..."); While (current! = Null) {FILIT (and buff, size (book), 1, outside); Current = current-> next; } Printf_s ("FULL! \ N"); Fclose (outside);   

book_open - takes an indicator into book

  int book_open (the structure book) * * Current, four * file_name) {FILE * in; Book * Buff; // An indicator with malloc'd memory - Can not reuse the local variable version! Book * vnext = * current; Int i; In = FOPN ("west", "rb"); // I hope this is the name of your file printf_s ("Reading database from% s ...", file_name); If (! In) {printf_s ("\ nERROR!"); Return 1; } While (1) {buf = malloc (sizeof (book)); I = FRED (& amp; BUFF, size (book), 1, in); If (FIF (in)) {free (buff); // never made it into brakes; } Buf-> Next = null; // The 'next' written to file is definitely not the same / if it is empty, then at the next point (* current == faucet) * current = buff; Else {wnext- & gt; Next = buff; Wnext = buf; // Next recurrence you would like buf- & gt; Next setting will be}} fclose (in); Printf_s ("done!"); Return 0; }  

I think that's better.


Comments