Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Cannot load text files to structure properly. (http://www.go4expert.com/forums/load-text-files-structure-properly-t28706/)

jose_peeterson 10Jul2012 21:49

Cannot load text files to structure properly.
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.


#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);

      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;
  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);
  // printf("\n%d",total_items);

return total_items;

Below is the contents of the items2.txt file.


eng_math cn_4 croft book learning xxxx 1 00/00/00
matlab cn_1 hines book learning xxxx 1 00/00/00

xpi0t0s 11Jul2012 12:02

Re: Cannot load text files to structure properly.
Remember the terminating NULL!

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.

jose_peeterson 11Jul2012 15:28

Re: Cannot load text files to structure properly.
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.


char availability[2];

xpi0t0s 11Jul2012 16:17

Re: Cannot load text files to structure properly.
Remember what I said before about terminating NULLs? You still have such a problem - now it's up to you to find it.

jose_peeterson 14Jul2012 07:52

Re: Cannot load text files to structure properly.
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!

All times are GMT +5.5. The time now is 22:49.