Thread: link list error

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    link list error

    anyone have any idea why this link list implementation isn't working. i've put a few print statements in the code to see where the problem lays but i've been looking at the code for awhile now.

    basically the file is opened or created if there isn't one. However it doesn't seem to update the information to the dynamically allocated structs. any help would be appreciated.

    Code:
    /*eno8p3 - link list implementation*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define SEAT_NUM 12
    #define MAX 20/*no one has more than 20 characters in their names */
    #define CHUNK 1 /*size of chunk to copy to file */
    #define CLIENTS "Clients.dat"/*client file for CAF */
    #define TAKEN 1
    #define DELETE 2
    #define AVAILABLE 0
    #define FLUSH  (while (getchar() !='\n') )
    
    typedef struct pass{
           int SeatNum;
           int SeatStatus;
           char LastName[MAX];
           char FirstName[MAX];
           struct pass* next_node;
           }CAF;
           
    const size_t SIZE = sizeof(CAF);     
           
    CAF *DataInit();/*intializes data from file saves to structs*/
    
    int main(void){
        DataInit();
        getchar();
        return 0;
    }
    
    
    CAF *DataInit(){ 
         FILE *fp;/*keeps addy of file looking for*/
         int ndx=0, seatNum,seatStatus;
         char firstName[MAX],lastName[MAX];
         CAF *pass,*prev,*head=NULL;
         
    /*attempts to open clients file and intialise data to Link List of CAF objects*/
         if ( (fp = fopen(CLIENTS,"rb") )== NULL){
             printf("%s was not available creating new file\n\n",CLIENTS);
             if( (fp=fopen(CLIENTS,"ab") )==NULL)
             perror("\aERROR");
             }
    
    /*go to biginning of file*/
         puts("Passenger file successfully opened.");
         rewind(fp);
    
    /*while there is actual data to be initialized*/
    while ( (fscanf(fp,"%d %d %s %s",&seatNum,&seatStatus,firstName,lastName)) == 4){
               /*malloc struct if no space end program*/
             if ( ( pass = malloc(SIZE) )==NULL){
               perror("alert:");
               exit(EXIT_FAILURE);
               }
               
            /*else assign values to current passenger*/
               pass->SeatNum=seatNum; 
               pass->SeatStatus = seatStatus;
               strcpy(pass->FirstName,firstName); puts(pass->FirstName);
               strcpy(pass->LastName,lastName);
               pass->next_node = NULL; /*close it off*/
             
              /*place struct in CAF list object*/
              if ( (head == NULL) )/*if its the first item in list head gets its value*/
                  head = pass;/*head points to the first item in the list which is an object*/
              
              else
              /*logic if its the second time, prev will already contain the addy of the first
              structure - assigned in the last statement, so now link the next struct and 
              that will be what next_node will contain the addy of the next passenger (pass). 
              on each successive iteration. next node will always contain the addy of the next
              object somewhere in memory.*/
               prev->next_node = pass;/*where pass is the current passenger object*/
                 
              /*next iteration passenger will actually be the previous so assign to prev*/
              prev = pass;/*so pass now in memory somewhere but no identifier but in mem*/
              printf("client %d initialized\n",ndx++);
    }
    /*end of while function close the file and exit*/ 
         if( (fclose(fp)) !=0)
          perror("\aERROR===");
          
          return head;
         
    }/*end of function*/
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    How did you create the file "Clients.dat"

    You reading the file in binary format.
    How did you create it ?

    I took your code and deleted from your struct the integers.
    with the file looking like this :
    Dan ben
    aaa bbb
    hellow world
    Dufi duck

    It worked fine for me.

    Code:
    #define SEAT_NUM 12
    #define MAX 20      /*no one has more than 20 characters in their names */
    #define CHUNK 1     /*size of chunk to copy to file */
    #define CLIENTS "Clients.dat"/*client file for CAF */
    #define TAKEN 1
    #define DELETE 2
    #define AVAILABLE 0
    #define FLUSH  (while (getchar() !='\n') )
    
    typedef struct pass{
    /*       int SeatNum; */
    /*       int SeatStatus;  */
           char LastName[MAX];
           char FirstName[MAX];
           struct pass* next_node;
           }CAF;
           
    const size_t SIZE = sizeof(CAF);     
           
    CAF *DataInit();/*intializes data from file saves to structs*/
    
    
    
    int main(void)
    {
    CAF   *head;
    CAF   *tmp;
    
    
    if ( head = DataInit() )
       for ( tmp = head ; tmp ; tmp = tmp->next_node)
           printf("last = %s first = %s\n", tmp->LastName, tmp->FirstName );
    
    
    getchar();
    return 0;
    }
    
    
    CAF *DataInit()
    { 
    FILE *fp;/*keeps addy of file looking for*/
    int ndx=0, seatNum,seatStatus;
    char firstName[MAX],lastName[MAX];
    CAF *pass,*prev,*head=NULL;
         
    /*attempts to open clients file and intialise data to Link List of CAF objects*/
    if ( (fp = fopen(CLIENTS,"rb") )== NULL)
       {
       printf("%s was not available creating new file\n\n",CLIENTS);
       if( (fp=fopen(CLIENTS,"ab") )==NULL)
             perror("\aERROR");
       }
    
    /*go to biginning of file*/
    puts("Passenger file successfully opened.");
    rewind(fp);
    
    /*while there is actual data to be initialized*/
    /* while ( (fscanf(fp,"%d %d %s %s",&seatNum,&seatStatus,firstName,lastName)) == 4) */
    while ( (fscanf(fp,"%s %s",firstName,lastName)) == 2) 
         {
         /*malloc struct if no space end program*/
         if ( ( pass = malloc(SIZE) )==NULL)
            {
            perror("alert:");
            exit(EXIT_FAILURE);
            }
               
         /*else assign values to current passenger*/
    /*     pass->SeatNum=seatNum;  */
    /*     pass->SeatStatus = seatStatus; */
         strcpy(pass->FirstName,firstName); 
         strcpy(pass->LastName,lastName);
         pass->next_node = NULL; /*close it off*/
             
         /*place struct in CAF list object*/
         if ( (head == NULL) )/*if its the first item in list head gets its value*/
            head = pass;/*head points to the first item in the list which is an object*/
              
         else
              /*logic if its the second time, prev will already contain the addy of the first
              structure - assigned in the last statement, so now link the next struct and 
              that will be what next_node will contain the addy of the next passenger (pass). 
              on each successive iteration. next node will always contain the addy of the next
              object somewhere in memory.*/
               prev->next_node = pass;/*where pass is the current passenger object*/
                 
         /*next iteration passenger will actually be the previous so assign to prev*/
         prev = pass;/*so pass now in memory somewhere but no identifier but in mem*/
         printf("client %d initialized\n",ndx++);
         }
    /*end of while function close the file and exit*/ 
         if( (fclose(fp)) !=0)
          perror("\aERROR===");
          
          return head;
         
    }/*end of function*/

    Remarks:
    a. If you fail to open the file in "rb" mode, what is the point opening it with mode "ab" ?
    b. If you just opened the file why do you use rewind ?

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    thanks for replying. I suppose this could be some nuissance as to how the info was generated. the orignal client.dat file was created from this same program but it used an array of structs to enter the information. i simply used that client.dat file and tried to read from it to make sure it was working.

    a. If you fail to open the file in "rb" mode, what is the point opening
    it with mode "ab" ?
    b. If you just opened the file why do you use rewind ?
    a. that was a brain fart from coding late one night where i thought it made sense
    b. if the file isn't availble it'll create it so i can write and read from it. This can be done with "ab" mode. in this case i'll still have to rewind the file if i want to read from the biginning. I put the rewind there as overkill. but thank you.

    as for removing the integers and it working. again, i'll code the input function and see what happens. cause i can't see anything wrong with the code.
    thank you
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM