1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

PLEASE TRY AND check if program stops UNEXPECTEDLY

Discussion in 'C' started by jose_peeterson, Dec 25, 2011.

  1. jose_peeterson

    jose_peeterson New Member

    USERNAME = 1
    PASSWORD = 1

    dear all,

    i am trying to make a simple digital library program for my church. i have done the "members part" where new members are added and leaving members are deleted. i still have to do the "cd/books part". this program is pretty LONG. i am not sure why but the code works sometimes but other times it just stops in the middle.

    you will need to just create a .txt file called members.txt in the same folder as your source file.

    here is the LONG code please tell me what you think!.

    NOTE - to delete a member i dont know how to remove that line from a text file that was once added to the file so i just used - - to replace the name and joined_date of the deleted member. someone suggested that i could use \b and string[0] = '\0' to erase the line altogether. i don't understand that so a new way to DELETE MEMBERS from the text file could be very helpful thanks.

    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<string.h>
    
    struct borrowed
    {
     char member_name[50];                 // to access from borrowed.txt file         
     char items_name[150];
     char type;
     char due_date[8];               
    };
    
    struct members
    {
     char name[50];
     char joined_date[8];
     struct borrowed borrowed_items[5];
    };
    
                                                                                                                            
    int security(char username[10],char password[10]);  // fucntion prototypes
    void store_in_memfile(char members_init[58]);       // stores name and joined date to file
    void delete_from_memfile(char members_name[50],struct members member[1000]);    // deletes the member
    int update_total_members(void);
    int load_members(struct members member[1000]);     // ONLY loads NAME and JOINED DATE to structure members, RETURNS total number of members 
    void show(void);
    
    int main()
    {
      char username[10],password[10];
      int op;
      
      char members_init[58],members_det[58];             // name and joined_date                                                                       
      char members_name[50];                             // name to DELETE
      int total_members;                                 // takes the value of line counter
      struct members member[1000];
      
      printf("\n\t\tCalvary Life Assembly Digital Library\n\n\n");
      
      while(1)                                      // security while loop
       {
        printf("Please enter your USERNAME and PASSWORD\n");
        strcpy(username,"x");                       // clear the u_n and p_w    
        strcpy(password,"x");
        scanf("%s%s",username,password); 
        if(security(username,password))
         {
         continue;
         }
        else
         {
          printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");  // hide the p_w
          printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
          printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
          printf("\nSECURITY CLEARED!\n\n"); 
          break;
         }
       }                                            // security while loop
     
     
      printf("\n\t\tCalvary Life Assembly Digital Library\n\n\n\n");
     
      while(1)
       {
        printf("\n\tPress 1 - ADD New MEMBER\n\n");
        printf("\tPress 2 - DELETE a MEMBER\n\n");
        printf("\tPress 3 - VIEW All members\n\n");
        printf("\tPress ctrl c - EXIT\n\n\n");
        printf("You Entered --> : ");
        scanf("%d",&op);    
        
         if(op == 1)
          {                                       // read from keyboard
             printf("\nEnter FULL NAME with _ for spaces AND(after a space) Today's date <dd/mm//yy>.\n");
             fflush(stdin);
             fgets(members_init,58,stdin);          
             fflush(stdin);
                                                                  
             store_in_memfile(members_init);      // append to members.txt file   
                                                          
          }
          
         if(op == 2) 
          {      
            printf("\n\tWARNING : You are about to DELETE a member.\n");
            printf("\nEnter the FULL NAME with _ for spaces.\n");
            fflush(stdin);
            scanf("%s",members_name);
           
            delete_from_memfile(members_name,member);    // delete a member, sending MEMBER NAME ONLY
            
          }
          
         if(op == 3) 
          {
           show();
          
          }
         if(op == 4) 
          {
          
          }
          
         total_members = update_total_members();       // update total_membters after store or delete members, printf("\n\ntotal members = %d",total_members);
         
         load_members(member);                         // load members NAME and JOINED DATE from file into structure
        /* test*/
           
         
       
       }
    
    
    system("pause > null");
    return 0;
    }                                                  // END OF MAIN    
    
    
    
    the individual functions.

    Code:
    int security(char username[10],char password[10])
    {
      if(!(strcmp(username,"1")) && !(strcmp(password,"1")))
       return 0;
      else
       return 1; 
    }
    
    

    Code:
    
    void store_in_memfile(char members_init[58])
    {
     FILE *members;
     
     members = fopen("members.txt","a");
     
     if(members == NULL)
      {
       printf("\n\tWARNING : UNABLE to open members.txt\n");
       return;
      }
                                                                
     fprintf(members,"%s",members_init);        // add the new member              
     fclose(members);  
                            
    return;                             
    }
    
    
    Code:
    
    int update_total_members(void)
    {
     FILE *members;
     int line_no=0;
     char members_det[58];
     
     members = fopen("members.txt","r");
     
     if(members == NULL)
      {
       printf("\n\tWARNING : UNABLE to open members.txt\n");
       return 0;
      }
    
     while(fgets(members_det,58,members))       // update total_members
      {
      line_no = line_no + 1;  
      }  
                                                                
     fclose(members); 
                              
    return line_no;                             // update total_members 
    }
    
    
    Code:
    
    void delete_from_memfile(char members_name[50],struct members member[1000])
    {                                           // members_name = MEMBERS NAME ONLY.          
     char members_det[58];                      // this string reads a line from file (here DUMMY)
     int total_members,ctr,i=1;                 // ctr is the line number in file.
     char members_del[58];                      // holds name and joined_date together from struct
     
     total_members = load_members(member);                     // load the structure from file
     
     FILE *members;
     
     members = fopen("members.txt","w");                       // write to file the DELETED member
     
     if(members == NULL)
      {
       printf("\n\tWARNING : UNABLE to open members.txt\n");
       return;
      }
       
      for(ctr=0;ctr<total_members;ctr++)
       {
         members_del[0] = '\0';                          // clears the string
                                                                            
        if(!(strcmp(members_name,(member[ctr].name))))   // use ctr as line number to write
         {
                                                         // ctr as member to be deleted.
            strcpy((member[ctr].name),"-");
            strcpy((member[ctr].joined_date),"-\n");             
                                                         /* test printf("\n%s%s\n",(member[ctr].name),(member[ctr].joined_date));*/     
         }
                                                         /* test else printf("\n%s%s\n",(member[ctr].name),members_name); */
                                                
          strcat(members_del,(member[ctr].name));        // here do the string concatanate for ALL strings.
          strcat(members_del," ");
          strcat(members_del,(member[ctr].joined_date));
           
          fprintf(members,"%s",members_del);             // write line by line(ctr as line number) to file
       
                                                         /* test printf("%s\n",members_del); */
          members_del[0] = '\0';                         // clears the string
       }
                                                         /* test printf("%s\n",members_del); */
     fclose(members);   
     
    total_members = load_members(member);                // load the structure from file
    return;
    }
    
    

    Code:
    
    int load_members(struct members member[1000])               // loads ONLY NAME and JOINED DATE to structure
    {
     FILE *members;
     char members_det[58];                                      // a line from file
     char *temp_name,*temp_joined_date;                         // strings to hold PARSE tokens
     int i=0;                                                   // struct counter
     
     members = fopen("members.txt","r");
     
     if(members == NULL)
      {
       printf("\n\tWARNING : UNABLE to open members.txt\n");
       return 0;
      }
      
     while(fgets(members_det,58,members))       // load a line into members_det string
      {
        temp_name = strtok(members_det," ");    // parse the string members_det into tokens, first token is from 1st char to " ".
        temp_joined_date = strtok(NULL,"");     // second token(string) is from char after " " to end of word/line.
        
        strcpy((member[i].name),temp_name);
        strcpy((member[i].joined_date),temp_joined_date);
        i++;
      }  
                                                // printf("\n%s  %s\n",(member[9].name),(member[9].joined_date));
                                                                     
     fclose(members); 
     
    return i;
    }
    
    
    Code:
    
    void show(void)
    {
     char members_det[58];                                      // a line from file              
     FILE *members;              
     
     members = fopen("members.txt","r");
     
     if(members == NULL)
      {
       printf("\n\tWARNING : UNABLE to open members.txt\n");
       return;
      }
      
        printf("\n\n\n\t\tNAME   JOINED-DATE\n");
        
      while(fgets(members_det,58,members))             // load a line into members_det string
       {   
        printf("\t\t%s",members_det);
     
       }
        printf("\n\n");
    
    fclose(members);
    return;
    }
    
    
     
    Last edited: Dec 25, 2011
  2. poornaMoksha

    poornaMoksha New Member

    Show us the output where is hangs...
     
  3. xpi0t0s

    xpi0t0s Mentor

    What input did you give it?
    What output did you get?
    What were the contents of the data file?

    You rely heavily on fixed length buffers, yet you have no code for preventing a buffer overflow. This I suspect is the cause of the problem (buffer overflows, that is. For example: char str[5]; strcpy(str,"Hello"); will overflow the buffer).
     

Share This Page