I want to write a program that read the binary file to linked-list, here is my code:
but I wasn't been lucky to run it, any suggestions?!!
I want to write a program that read the binary file to linked-list, here is my code:
but I wasn't been lucky to run it, any suggestions?!!
I would recommend that you break up your tasks into individual programs so that you can more easily determine what's wrong. Both your file reading and linked list processing are incorrect. Write one program to build a linked list without having to worry about data input, and another program to read data from the file and simply print it out to verify that you read what you want.
Once you know that both programs work, you can integrate them into a single program.
My best code is written with the delete key.
What I sent you is just a few segment of my projct, just to show you part of the implementation, the program read some data but was incorrect for some reason. I think using the the following statments is not correct:
Code:while(fread(&record , sizeof(record), 1 ,fpp)) { pos =(long)i * sizeof(record); i++; fseek(fpp, pos,SEEK_CUR); current = (struct record*) malloc (sizeof(struct record)); if(head ==NULL) head = current; else prev->next = current; current -> next = NULL; prev = current; if (head == NULL) printf("No data read?!!"); }
Compare and contrast:
Code:#include <stdio.h> #include <stdlib.h> struct record { int stock_no; struct record *next; }; int main ( void ) { struct record *list = NULL; struct record *node, *save; FILE *inb; int i; inb = fopen ( "testfile", "rb" ); if ( inb == NULL ) return EXIT_FAILURE; while ( fread ( &i, sizeof ( int ), 1, inb ) == 1 ) { node = malloc ( sizeof *node ); if ( node == NULL ) break; node->stock_no = i; node->next = list; list = node; } for ( node = list; node != NULL; node = save ) { save = node->next; printf ( "%d ", node->stock_no ); free ( node ); } return 0; }
My best code is written with the delete key.
Thans for your help. But the problem still exists,
Since reading the same file to an array of structures was successful, but reading it to linked-list gives incorrect result due to the next field that has been added to the structure, as follows:
Here is my structure:
seems there is a shift in reading the data due to the "next" effect.Code:struct record{ int item_no; ........ //others struct record * next; // the problem appears after adding this field?!!!!!!!!! } //here is the function to read from “stock.dat” file. int read_file (void) { int i,j; int pos; i=0; rewind(fpp); //fseek(fpp, 0L,SEEK_SET); for(j=0;j<MAX_NO_ITEM;j++) { current = (struct record *)malloc(sizeof(struct record)); if(head ==NULL) head = current; else prev->next = current; current -> next = NULL; pos =(long)i * sizeof(record); i++; fseek(fpp, pos,SEEK_SET); fread(&record, sizeof (struct record),1,fpp); prev = current; if (head == NULL) printf("No data read?!!"); } current = head; while (current != NULL) { printf("%d\n",current->description); current = current -> next; } return i; //return number of items have been added }
Well that's easy. If you're writing your data in its entirety to the file, just do:
Lather, rinse, repeat.Code:...assuming a pointer called "list"... struct mystruct *tmp = NULL; tmp = malloc( sizeof(*tmp) ); if( tmp ) { if( fread( tmp, sizeof(*tmp), 1, yourfile ) == 1 ) { tmp->next = list; list = tmp; } else yourerrorfunction("Error reading..."); } else yourerrorfunction("Error mallocing...");
In short, all you have to do is manually set up your next pointers. I prepend, you could easily append. Whatever.
Quzah.
Hope is the first step on the road to disappointment.
>but reading it to linked-list gives incorrect result due to the next field that has been added to the structure
>fread(&record, sizeof (struct record),1,fpp);
And what makes you think that any pointers will contain meaningful values when you read them from a file? Pointers by definition contain non-persistent memory addresses. By writing them to a file, you gain nothing. Instead, write the data of each record and when you read it back, rebuild the links.
My best code is written with the delete key.