?
Solved

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

Posted on 2003-03-25
4
Medium Priority
?
263 Views
Last Modified: 2010-04-15
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
Comment
Question by:darkrain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Expert Comment

by:svatOpluk
ID: 8207356
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
 
LVL 5

Accepted Solution

by:
Kocil earned 300 total points
ID: 8208213
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
 
LVL 20

Expert Comment

by:jmcg
ID: 10194564
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question