Link to home
Start Free TrialLog in
Avatar of darkrain
darkrain

asked on

Creating a self ordering link list that can take in some contents from a text file and also be added to durring run time

I have been trying to create a link list that can be called a program start to take the contents from a text file and store it in the list in order until the end of the file is reached. Then i want to be able to add to it durring run time. I have created the structure and a link list but it read the first item and then the second overwrites the first, then it gets stuck in a loop. If the text file is empty my program then starts but when i want to add to it i get a segmentation fault and the program crashes.

This is what i have so far...
typedef struct clubber_s {
     char sClubberName[21];     /* Variable to store the clubbers WhoIs Code */
     int iIdentNo;               /* Variable to store the ident number */
     int iFeePaid;               /* Varibale to store which Fee Paid */
     int iCoins;                    /* Variable to store number of coins a clubber has */
     int iClubberInRoom;          /* Variable to store where a clubber is */
     struct clubber_s *link_p;
}clubber_t;

head_p = (clubber_t *)malloc(sizeof(clubber_t));
head_p = NULL;

inf_ClubberTxt(head_p);

void inf_ClubberTxt(clubber_t *head_p, RoomMaxCaps_t *preClub, RoomMaxCaps_t *lower, RoomMaxCaps_t *upper)
     {
          int iPreCount=0, iLowerCount=0, iUpperCount=0;
          int iCloseSucess =0;
          FILE *infClubber;
          infClubber = fopen("Clubber.txt" , "r");

          if (infClubber == NULL)
          {
               infClubber = fopen("Clubber.txt" , "w");
               iCloseSucess = fclose(infClubber);
               if (iCloseSucess ==0)
               {
                    printf("\nClubber.txt File Created");
                    return;
               }
               else
               {
                    printf("\nClubber.txt File Created but did not close properly\n");
                    printf("Program Halted");
                    exit(1);
               }
          }
          else
          {

               clubber_t *temp_p, *current_p;
               head_p=temp_p;
               temp_p=(clubber_t*)malloc(sizeof(clubber_t));
               current_p=(clubber_t*)malloc(sizeof(clubber_t));
               while (!(feof(infClubber)))
               {


                    /* if (temp_p == NULL)
                    {
                         printf("Out of Memory!");
                         exit(1);
                    } */
                    if (head_p == NULL)
                    {
                         /*head_p=(clubber_t*)malloc(sizeof(clubber_t));*/
                         fscanf(infClubber,"%d", &temp_p->iIdentNo);
                         fscanf(infClubber,"%s", temp_p->sClubberName);
                         fscanf(infClubber,"%d", &temp_p->iFeePaid);
                         fscanf(infClubber,"%d", &temp_p->iCoins);
                         fscanf(infClubber,"%d", &temp_p->iClubberInRoom);
                         temp_p->link_p=NULL;
                         current_p->link_p=temp_p;
                    }
                    else
                    {

                         current_p = head_p;
                         while (current_p->link_p != NULL)
                         {
                              current_p = current_p->link_p;
                         }

                         fscanf(infClubber,"%d", &temp_p->iIdentNo);
                         fscanf(infClubber,"%s", temp_p->sClubberName);
                         fscanf(infClubber,"%d", &temp_p->iFeePaid);
                         fscanf(infClubber,"%d", &temp_p->iCoins);
                         fscanf(infClubber,"%d", &temp_p->iClubberInRoom);

                                        temp_p->link_p=NULL;
                         current_p->link_p=temp_p;
                                         }
return;
}

If anyone can come up with some better code i will be for ever gratefull.

Thanks in advance.
Avatar of svatOpluk
svatOpluk

This code is messed up. The problem is in your loop while(!feof). You have to malloc your cluber_t list member every time you write one clubber. You have made only two memory blocks for storing your inputs. I hadnt examine the whole code so there can be more errors.
ASKER CERTIFIED SOLUTION
Avatar of Kocil
Kocil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by Kocil.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer