Thread: display copied data into structs

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    display copied data into structs

    Hey guys im trying to test whether my strcpy is actually copying the data into the structs properly. But it wont disply anything how come?

    Code:
    int loadData(GJCType* menu, char* menuFile, char* submenuFile)
    {
       /*pointer for malloc*/
       CategoryType *newCat, *currentCat, *prevCat;
       
       
    
       /*declaration of variables*/
       int CountNoLines = 0;
       char temp[LINE_LENGTH];
       
       /* variables for tokenizer for menu file*/
       char *catID;
       char *catType;
       char *catName;
       char *catDescription;
       char *delim;
       
       /* declare file pointers to files*/
       FILE *f1;
       FILE *f2;
       
       
       /* opening the menu and submenu for reading*/
       f1 = fopen(menuFile, "r");
       f2 = fopen(submenuFile, "r");
       
       /* check to see if the filuse exists*/
       if(f1 ==NULL || f2 == NULL)
       {
          /* check whether both files have existed or typed in correctly*/
          if(f1 == NULL && f2 == NULL)
          {
          
             printf("Both files do not exist %s %s\n",menuFile,submenuFile);
          }
          /* check for each file existance*/
          else if (f1 == NULL)
          {
             printf("%s does not exist\n\n",menuFile);
          }
          /* check for each file existance*/
          else if(f2 == NULL)
          {
             printf("%s does not exist\n\n", submenuFile);
    	 
          } 
          printf("EXITING PROGRAM ERROR!\n\n");
          return ERRORCODE; /* cannot proceed*/
       }
       
       /* counts how many fields there are in the file*/
       while(fgets(temp, LINE_LENGTH, f1)!=NULL)
       {
           
           temp[strlen(temp)]= '\0';
           printf("%s", temp);
           
           newCat = malloc(sizeof(CategoryType));
           
           if(prevCat == NULL)
           {
               menu->headCategory = newCat;
           }
           else
           {
           
               
            
               prevCat->nextCategory = newCat;
           
         
       
               /* stores the catID but also checks if there
    	   is a duplicate*/
    	    
               catID = strtok(temp, "|");
    	   
    	   /* checking whether it tokenizez*/
    	   if(catID == NULL)
    	   {
    	      printf("CatID missing\n");
    	   }
    	   
    	   /*checks for a duplication of the id field*/
    	   if(!checkDuplicationID(menu, catID))
    	   {
    	      return ERRORCODE;
    	   }
               /* gets the second field and tokenizez it*/
    	   catType = strtok(NULL, "|");
    	   
    	    /* checking whether it tokenizez*/
    	   if(catType == NULL)
    	   {
    	      printf("CatType missing\n");
    	   }
    	   
    	   catName = strtok(NULL, "|");
    	   
    	    /* checking whether it tokenizez*/
    	   if(catName == NULL)
    	   {
    	      printf("CatName missing\n");
    	   }
    	   catDescription = strtok(NULL, "\0");
    	   
    	    /* checking whether it tokenizez*/
    	   if(catDescription == NULL)
    	   {
    	      printf("CatDescription missing\n");
    	   }
    	   
    	   /* checks the string length of the field*/
    	   if((strlen(catID) >ID_LEN) ||
    	      (strlen(catType) >MAX_NAME_LEN)  ||
    	      (strlen(catDescription) >MAX_DESC_LEN))
    	      
    	   {	   
    	   
    	      printf("Wrong data format\n\n");
    	      return ERRORCODE;
    	      
    	   }
    	   
    	   strcpy(newCat->categoryID, temp);
    	   newCat->drinkType = temp[0];
    	   strcpy(newCat->categoryName, temp);
    	   strcpy(newCat->categoryDescription, temp);
    	   
    	   
    	   
    	   newCat -> nextCategory = NULL;
    	   newCat->headItem = NULL;
    	   newCat->numItems = 0;
    	   prevCat = newCat;
    	   
    	   currentCat = menu->headCategory;
    	   
    	}/* end of if else used to tokenize and copy variables
    	  from and into structs*/     
    	   while(currentCat!=NULL)
    	  {
    	     printf("%s, %c, %s, %s", currentCat->categoryID,
    	     currentCat->drinkType, currentCat->categoryName,
    	     currentCat->categoryDescription);
    	     
    	     currentCat = currentCat->nextCategory;
    	  }
    	
    	   
    	
    	  
       } /* end of while loop*/   
          
       
       
       /* close both files after reading*/
       if(fclose(f1)!=0 || fclose(f2)!=0)
       {
          fprintf(stderr, "Error in closing files\n");
       }   
      
    
       return EXIT_SUCCESS;
    }

    Code:
    #ifndef GJC_H
    #define GJC_H
    
    /* System-wide header files. */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /* System-wide constants. */
    #define ID_LEN 5
    #define MIN_NAME_LEN 1
    #define MAX_NAME_LEN 25
    #define MIN_DESC_LEN 1
    #define MAX_DESC_LEN 250
    #define NUM_PRICES 3
    #define HOT 'H'
    #define COLD 'C'
    #define ERRORCODE 1
    #define VALID 0
    #define LINE_LENGTH 500
    #define TOKEN_PRODUCT 4
    
    typedef struct category* CategoryTypePtr;
    typedef struct item* ItemTypePtr;
    
    /* Structure definitions. */
    typedef struct price
    {
       unsigned dollars;
       unsigned cents;
    } PriceType;
    
    typedef struct item
    {
       char itemID[ID_LEN + 1];
       char itemName[MAX_NAME_LEN + 1];
       PriceType prices[NUM_PRICES];
       char itemDescription[MAX_DESC_LEN];
       ItemTypePtr nextItem;
    } ItemType;
    
    typedef struct category
    {
       char categoryID[ID_LEN + 1];
       char categoryName[MAX_NAME_LEN + 1];
       char drinkType;      /* (H)ot or (C)old. */
       char categoryDescription[MAX_DESC_LEN];
       CategoryTypePtr nextCategory;
       ItemTypePtr headItem;
       unsigned numItems;
    } CategoryType;
    
    typedef struct gjc
    {
       CategoryTypePtr headCategory;
       unsigned numCategories;
    } GJCType;
    
    int commandLineArguments(int argc, char* argv[]);
    int countToken(FILE *fp, char* temp, int tokenPerLine);
    #endif

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    temp[strlen(temp)]= '\0';
    You probably meant
    Code:
    temp[strlen(temp)-1] = '\0';
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by dwks
    Code:
    temp[strlen(temp)]= '\0';
    You probably meant
    Code:
    temp[strlen(temp)-1] = '\0';
    Are you shure that this makes sense at all ? this would just replace the '\0' with a fresh one.
    Kurt

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's why I said to add a -1.

    [edit]
    Since strlen("") is 0, s[strlen(s)] must be the terminating NULL character. To replace the NULL with another NULL, as s[strlen(s)] = 0 would do, seems silly. What you should do is replace the character before the NULL (presumably a '\n') with a NULL as in s[strlen(s)-1] = 0.
    [/edit]
    Last edited by dwks; 05-02-2006 at 02:06 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by dwks
    That's why I said to add a -1.
    Yeah but because of the use of strlen() it must already be there.
    Kurt

    EDIT: Sorry my fault, take everything back.
    Last edited by ZuK; 05-02-2006 at 02:11 PM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I didn't mention that the point of the code was to eliminate a '\n'.

    TTFN
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Half my function is lazy...
    By Shadow in forum C Programming
    Replies: 0
    Last Post: 10-09-2001, 09:46 AM