• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

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.
0
darkrain
Asked:
darkrain
1 Solution
 
svatOplukCommented:
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.
0
 
KocilCommented:
typedef struct clubber_s {
    char sClubberName[21];
    /* deleted for brevity */
    struct clubber_s *link_p;
} clubber_t;

#define IsEmpty(head) (head == NULL)

// Compare 2 clubbers, you may want to modify it
// return 1 : c1 > c2
//        0 : c1 == c2
//        -1 : c1 < c2
int Compare(clubber_t *c1, clubber_t *c2)
{
   return strcmp(c1->sClubberName, c2->sClubberName);
}
 
// insert ascending
void InsertSort(clubber_t *head, clubber_t *item)
{
   clubber_t *p1, *p2;
   if (IsEmpty(head)) {
       head = item;
       item->link_p = NULL;
   }
   else {
       // find the right position to insert
       p1 = head; p2 = head->link_p;
       while((p2 != NULL) && (Compare(item, p2) < 0)) {
           p1 = p2; p2 = p1->link_p;
       }
       // got it
       p1->link_p = item;
       item->link_p = p2;
   }
}

Delete(clubber_t *head)
{
   clubber_t *p;
   while (head) {
      p = head->list_p;
      free(head);
      head=p;
   }
}



InsertFromFile(clubber_t *head, char *fname)
{
    FILE* f;
    clubber_t item;

    f = fopen(fname, "r");
    for(;;) {
       item = malloc(sizeof(clubber_t));
       if (fscanf(f, "%s", item->sClubberName) != EOF) {
          InsertSort(head, item);
       }
       else {
          free(item); break;
       }
     }
     fclose(f);  
}


main()
{
   clubber_t *head;

   InsertFromFile(head, "CLUB.TXT");
   ...
   Delete(head);
}

0
 
jmcgOwnerCommented:
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now