Thread: puplate a linked-list from binary file

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    11

    puplate a linked-list from binary file

    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?!!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    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?!!");
    
            }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    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:
    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
    
     
    
    }
    seems there is a shift in reading the data due to the "next" effect.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well that's easy. If you're writing your data in its entirety to the file, just do:
    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...");
    Lather, rinse, repeat.

    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.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. help me debug this linked list !
    By Dark Angel in forum C Programming
    Replies: 6
    Last Post: 04-18-2008, 02:10 PM
  3. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 06:09 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM