!HELP! : Unable to print desired strings to text file!

Discussion in 'C' started by jose_peeterson, Jul 4, 2012.

  1. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    Dear all,
    In the following program, I am trying to delete a line from a text file called items.txt
    the line in the file has 8 strings seperated by a space. I first load these strings into an array of structure called items. Then i try to delete a line from the text file by entering the first string of the line. this should delete the entire line from the text file. To implement this. I first load the items from the items.txt file to a structure using the load_items(item); function. then in the delete function i have to key in the first string of a line that i want to delete. this string is compared with the string in the structure(field called name). if they match then this line is NOT printed onto the file while other lines are printed by first concatenating all the 8 various strings into 1 string and using fprintf() to write back to the text file. Note that load_items(item); returns the number of lines in the items.txt file.

    The error occurs when i write it back after deleting a line. PLEASE HELP ME. THX!

    Code:
    
                       
                                  /* ADD ITEMS*/
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct items
    {
     char name[100];             
     char call_no[10];
     char author[100];
     char type[10];
     char borrower[100];
     char availability[1];
     char due_date[8];
     char genre[15];
     
    };
    
    
    int save_in_itemfile(char item_details[235]);
    int load_items(struct items item[5500]);
    int delete_from_itemfile(char delete_item_name[100],struct items item[5500]);
    
    
    int main()
    {
     struct items item[5500];
     
     char delete_item_name[100];
     int total_items;
    
     char item_details[235]; // all details of item
      
    /*  
      printf("Type the name of the item with _ for spaces and after a space\n");
      printf("type the call number of the item with _ for spaces and after a space\n");
      printf("type the name of the author with _ for spaces and after a space\n");
      printf("type the type of the item with _ for spaces and after a space\n");
      printf("type the genre of the item with _ for spaces. Hit ENTER\n");
      fflush(stdin);
      fgets(item_details,235,stdin);
      fflush(stdin);                         //  printf("\n%s",item_details);
      
      
      item_details[strlen(item_details) - 1] = '\0'; // prevent fgets from storing the \n character
      
                                             //  printf("\n%s",item_details);
      
      strcat(item_details," ");
      strcat(item_details,"xxxx");
      strcat(item_details," ");
      strcat(item_details,"1");
      strcat(item_details," ");
      strcat(item_details,"00/00/00");
      strcat(item_details,"\n");  // extra character to the string length (+1)
      
     // printf("\n%s %d",item_details,strlen(item_details));
    
      save_in_itemfile(item_details);
    */
     
      total_items = load_items(item);
     
     
      // printf("%d\n",total_items);
     
     
    //  printf("%s",item[total_items].name);
     
    
     printf("\n\n\tWARNING : you are about to DELETE an Item !!\n");
     printf("\nType the name of the item to DELETE\n");
     fflush(stdin);
     scanf("%s",delete_item_name);
     fflush(stdin);
    
      delete_from_itemfile(delete_item_name,item);
      
    system("pause > null");
    return 0;
    }
    
    int save_in_itemfile(char item_details[235])
    {
      FILE *items;  
      items = fopen("items.txt","a");
      
      if(items == NULL)
       {
        printf("ERROR : unable to open items.txt\n");
        return 0;
       }
      
      if(item_details[0] != '\n')         // capture the ENTER KEY and prevent it written to file.                                                            
      {
       fprintf(items,"%s",item_details);  // add the new member              
      }
      
    fclose(items); 
    return 0;
    }
    
    
    
    int load_items(struct items item[5500])
    {
     int total_items=0;
     char *string;              // string tokens of item_details
     char item_details[235];
     FILE *items;
      
     items = fopen("items.txt","r");
    
     if(items == NULL)
      {
       printf("ERROR : Unable to open file!\n");          
       return 0;
      }
      
      
                                                  ;  
     while(fgets(item_details,235,items))  
      {                                                 
      
       string = strtok(item_details," ");   
       strcpy((item[total_items].name),string);  // printf("%s\n",item[total_items].name);
                                            
       string = strtok(NULL," ");
       strcpy((item[total_items].call_no),string);  
                                 
       string = strtok(NULL," ");   
       strcpy((item[total_items].author),string); 
                    
       string = strtok(NULL," ");
       strcpy((item[total_items].type),string); 
       
       string = strtok(NULL," "); 
       strcpy((item[total_items].genre),string);  
                      
       string = strtok(NULL," ");
       strcpy((item[total_items].borrower),string); 
                       
       string = strtok(NULL," ");
       strcpy((item[total_items].availability),string);  
                       
       string = strtok(NULL,"\n");      // \n is the last characcter in item_details
       strcpy((item[total_items].due_date),string);  
      
        total_items++;
      }   
      
      // printf("\n%d",total_items);
     
    fclose(items);
    return total_items;
    }
    
    
    
    int delete_from_itemfile(char delete_item_name[100],struct items item[5500])
    {
      int total_items,ctr;
      FILE *items;
      char item_details[235]; 
      
      total_items = load_items(item);
      
      items = fopen("items.txt","w");
      
      if(items == NULL)
       {
        printf("ERROR : Unable to open item.txt !\n");
        return 0;           
       }
       
      for(ctr=0;ctr<total_items;ctr++)
       {                              
                                    //  printf("%s\n",item[ctr].name);
      if(!(strcmp(delete_item_name,(item[ctr].name))))
        {
          
           printf("\nMatched and deleted\n");
      //  printf("%d ",ctr);
          continue;
        }
       
        {
            //   printf("%d ",ctr);              
                             
         strcat(item_details,item[ctr].name);
         strcat(item_details," ");
         strcat(item_details,item[ctr].call_no);
         strcat(item_details," ");
         strcat(item_details,item[ctr].author);
         strcat(item_details," ");
         strcat(item_details,item[ctr].type);
         strcat(item_details," ");
         strcat(item_details,item[ctr].genre);
         strcat(item_details," ");
         strcat(item_details,item[ctr].borrower);
         strcat(item_details," ");
         strcat(item_details,item[ctr].availability);
         strcat(item_details," ");
         strcat(item_details,item[ctr].due_date);
         strcat(item_details,"\n");
         
        // printf("%s\n",item_details,strlen(item_details));
        fprintf(items,"%s",item_details);
        
        } 
       
       }
        
    fclose(items);  
    
    load_items(item);
    return 0;
    }
    
    
    the text file called items.txt has the following info:

    Code:
    bill withers a lovely day xxxx 1 00/00/00
    always seems to know the  xxxx 1 00/00/00
    then one look at you xxxx 1 00/00/00
    its gonna be lovely day xxxx 1 00/00/00
    ahead of you seemed impossible xxxx 1 00/00/00
    
    
     
  2. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    Hi all, I have slightly modified the code but the Problem is NOT fixed yet Please help me!

    The new code is as follows, you may IGNORE the old code above. but the text file remains the same

    Code:
                       
                                  /* ADD ITEMS*/
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct items
    {
     char name[100];             
     char call_no[10];
     char author[100];
     char type[10];
     char borrower[100];
     char availability[1];
     char due_date[8];
     char genre[15];
     
    };
    
    
    int save_in_itemfile(char item_details[235]);
    int load_items(struct items item[5500]);
    int delete_from_itemfile(char delete_item_name[100],struct items item[5500]);
    
    
    int main()
    {
     struct items item[5500];
     
     char delete_item_name[100];
     int total_items;
    
     char item_details[235]; // all details of item
      
    /*
      printf("Type the name of the item with _ for spaces and after a space\n");
      printf("type the call number of the item with _ for spaces and after a space\n");
      printf("type the name of the author with _ for spaces and after a space\n");
      printf("type the type of the item with _ for spaces and after a space\n");
      printf("type the genre of the item with _ for spaces. Hit ENTER\n");
      fflush(stdin);
      fgets(item_details,235,stdin);
      fflush(stdin);                         //  printf("\n%s",item_details);
      
      
      item_details[strlen(item_details) - 1] = '\0'; // prevent fgets from storing the \n character
      
                                             //  printf("\n%s",item_details);
      
      strcat(item_details," ");
      strcat(item_details,"xxxx");
      strcat(item_details," ");
      strcat(item_details,"1");
      strcat(item_details," ");
      strcat(item_details,"00/00/00");
      strcat(item_details,"\n");  // extra character to the string length (+1)
      
     // printf("\n%s %d",item_details,strlen(item_details));
    
      save_in_itemfile(item_details);
    */
     
      total_items = load_items(item);
     
     
      // printf("%d\n",total_items);
     
     
    //  printf("%s",item[total_items].name);
     
    
     printf("\n\n\tWARNING : you are about to DELETE an Item !!\n");
     printf("\nType the name of the item to DELETE\n");
     fflush(stdin);
     scanf("%s",delete_item_name);
     fflush(stdin);
    
      delete_from_itemfile(delete_item_name,item);
    
    
    system("pause > null");
    return 0;
    }
    
    int save_in_itemfile(char item_details[235])
    {
      FILE *items;  
      items = fopen("items.txt","a");
      
      if(items == NULL)
       {
        printf("ERROR : unable to open items.txt\n");
        return 0;
       }
      
      if(item_details[0] != '\n')         // capture the ENTER KEY and prevent it written to file.                                                            
      {
       fprintf(items,"%s",item_details);  // add the new member              
      }
      
    fclose(items); 
    return 0;
    }
    
    
    
    int load_items(struct items item[5500])
    {
     int total_items=0;
     char *string;              // string tokens of item_details
     char item_details[235];
     FILE *items;
      
     items = fopen("items.txt","r");
    
     if(items == NULL)
      {
       printf("ERROR : Unable to open file!\n");          
       return 0;
      }
      
      
                                                  ;  
     while(fgets(item_details,235,items))  
      {                                                 
      
       string = strtok(item_details," ");   
       strcpy((item[total_items].name),string);  // printf("%s\n",item[total_items].name);
                                            
       string = strtok(NULL," ");
       strcpy((item[total_items].call_no),string);  
                                 
       string = strtok(NULL," ");   
       strcpy((item[total_items].author),string); 
                    
       string = strtok(NULL," ");
       strcpy((item[total_items].type),string); 
       
       string = strtok(NULL," "); 
       strcpy((item[total_items].genre),string); // printf("%s",item[total_items].genre);
                      
       string = strtok(NULL," ");
       strcpy((item[total_items].borrower),string); 
                       
       string = strtok(NULL," ");
       strcpy((item[total_items].availability),string);  
                       
       string = strtok(NULL,"\n");      // \n is the last characcter in item_details
       strcpy((item[total_items].due_date),string);  
      
        total_items++;
      }   
      
      // printf("\n%d",total_items);
     
    fclose(items);
    return total_items;
    }
    
    
    
    int delete_from_itemfile(char delete_item_name[100],struct items item[5500])
    {
      int total_items,ctr;
      FILE *items;
      char item_details[235]; 
      item_details[0] = '\0';                        // clear the string
      
      total_items = load_items(item);
                                                  //   printf("%s",item[3].genre);          
      items = fopen("items.txt","w");
      
      if(items == NULL)
       {
        printf("ERROR : Unable to open item.txt !\n");
        return 0;           
       }
       
      for(ctr=0;ctr<total_items;ctr++)
       {                              
                                    //  printf("%s\n",item[ctr].name);
      if(!(strcmp(delete_item_name,(item[ctr].name))))
        {
          
           printf("\nMatched and deleted\n");
      //  printf("%d ",ctr);
          //continue;
        }
       else
        {
            //   printf("%d ",ctr);              
                             
         strcat(item_details,item[ctr].name);
         strcat(item_details," ");            
         strcat(item_details,item[ctr].call_no);
         strcat(item_details," ");              
         strcat(item_details,item[ctr].author);
         strcat(item_details," ");             
         strcat(item_details,item[ctr].type);
         strcat(item_details," ");           
         strcat(item_details,item[ctr].genre);
         strcat(item_details," ");        //    printf("%s",item[ctr].genre);
         strcat(item_details,item[ctr].borrower);
         strcat(item_details," ");               
         strcat(item_details,item[ctr].availability);
         strcat(item_details," ");                   
         strcat(item_details,item[ctr].due_date);
         strcat(item_details,"\n");              
                                   
        
        fprintf(items,"%s",item_details);
       item_details[0] = '\0';                // clear the string each time
        
        } 
       
      /*    if(ctr == 1)
           {
            break;
           }
      */    
       }
        
    fclose(items);  
    
    load_items(item);
    return 0;
    }
    
    
    
    The genre of the structure does not get displayed in the text file.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice