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

Cannot load text files to structure properly.

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

  1. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    Dear ALL,
    I am trying to load a few lines ( many strings seperated by a space) from a text file and break them into string tokens and store it as structure fields. This functions should be performed by the load_items(item); function.
    However there is an anomaly. When i print the structure fields to check if they have been loaded properly, it turns out they are not!. when i print structure fields outside the load_items(item); function the fields genre, borrower, availability, due_date do not seem to be stored properly in the array.


    The following code is the main function and the load_items function.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct items
    {
     char name[10];             
     char call_no[10];
     char author[10];
     char type[10];
     char borrower[10];
     char availability[1];
     char due_date[8];
     char genre[10];
     
    };
     
    
    int load_items(struct items item[5500]);
    
    
    int main()
    {
    
     struct items item[3];
     int total_items;
    
      total_items = load_items(item);
      // printf("\n%d\n",total_items);
    
    for(ctr=0;ctr<total_items;ctr++)
       {   
          printf("\n%s ",item[ctr].name);
          printf("%s ",item[ctr].call_no);
          printf("%s ",item[ctr].author);
          printf("%s ",item[ctr].type);
          printf("%s ",item[ctr].genre);            // error
          printf("%s ",item[ctr].borrower);
          printf("%s ",item[ctr].availability);      // error
          printf("%s\n",item[ctr].due_date);         // error
       }
    
    system("pause > null");
    return 0;
    }
    
    
    int load_items(struct items item[3])
    {
     int total_items=0;
     char *string;              // string tokens of item_details
     char item_details[69];
     int ctr;
     FILE *items;
      
     items = fopen("items2.txt","r");
    
     if(items == NULL)
      {
       printf("ERROR : Unable to open file!\n");          
       return 0;
      }
      
      
                                                    
     while(fgets(item_details,69,items))  
      {                                                 
      
       string = strtok(item_details," ");   
       strcpy((item[total_items]).name,string);   printf("%s ",item[total_items].name);
                                            
       string = strtok(NULL," ");
       strcpy((item[total_items]).call_no,string);  printf("%s ",item[total_items].call_no);
                                 
       string = strtok(NULL," ");   
       strcpy((item[total_items]).author,string); printf("%s ",item[total_items].author);
                    
       string = strtok(NULL," ");
       strcpy((item[total_items]).type,string); printf("%s ",item[total_items].type);
       
       string = strtok(NULL," "); 
       strcpy((item[total_items]).genre,string);   printf("%s ",item[total_items].genre);
                      
       string = strtok(NULL," ");
       strcpy((item[total_items]).borrower,string); printf("%s ",item[total_items].borrower);
                       
       string = strtok(NULL," ");
       strcpy((item[total_items]).availability,string);   printf("%s ",item[total_items].availability);
                       
       string = strtok(NULL,"\n");      // \n is the last characcter in item_details
       strcpy((item[total_items].due_date),string);   printf("%s\n",item[total_items].due_date);
      
      
        total_items++;
        
      }   
        
      // printf("\n%d",total_items);
    
    
    fclose(items);
    return total_items;
    }
    
    
    

    Below is the contents of the items2.txt file.

    Code:
    eng_math cn_4 croft book learning xxxx 1 00/00/00
    matlab cn_1 hines book learning xxxx 1 00/00/00
    
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Remember the terminating NULL!
    Code:
    char availability[1];
    
    only has room for that terminating NULL. If you try to store "x" in this, remember that is actually a TWO-char array of 'x' and '\0' so you will get a buffer overflow and undefined behaviour.
     
    shabbir likes this.
  3. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    Dear Mr xpiotos,
    Like you had told me, I changed availability[1] to 2. so now the outcome is better but it the genre from the string token does not seem to get into the stucture field genre. You can notice the missing word "learning" in the print out . why is that? I am not sure, Please lend a hand.
    THANKS.

    Code:
    char availability[2];
     
    Last edited: Jul 11, 2012
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Remember what I said before about terminating NULLs? You still have such a problem - now it's up to you to find it.
     
    shabbir likes this.
  5. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    Thanks Mr xpiotos, You were absolutely right. That was the problem. for example due date (00/00/00)was only give 8 spaces but in fact it needed 9. After fixing this it worked. great work. Thanks Again. SILLY ME!
     

Share This Page