1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Part of the unfinished VIRTUAL LIBRARY program.

Discussion in 'C' started by jose_peeterson, Jun 9, 2012.

  1. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    Hi all,
    I am planning to digitise my church library so am using C to code a virtual library. I am quite behind schedule but i am refreshing my C lanugage after a long break. I will post my previous work here. Overall i have just written the code to add and delete members from a text document. so at the moment a new member can be added and saved to text file and loaded to the program anytime and also deleted from the file. I will begin to work on the books borrowing and cd borrowing by next week once i Recap all the C jargon. For now please try the following code and CHANGE IT IN WHATEVER WAY YOU LIKE! It's yours.

    <small glitch, Program stops unexpectedly, This is one f*cking problem, the cause of which i can't identify, DAMN>


    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           
    
    int security(char username[10],char password[10])
    {
      if(!(strcmp(username,"1")) && !(strcmp(password,"1")))
       return 0;
      else
       return 1; 
    }
    
    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;                             
    }
    
    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 
    }
    
    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;
    }
    
    
    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;
    }
    
    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;
    }
    
    
    
    
     

Share This Page