Search code examples
cstructtokenstrtolstrtoull

token to unsigned printing odd values


As the title suggested I am getting some weird returns from strtoul, I have tested just by using strcpy to store the data as a string and it is giving the correct value, but as soon as I try and change it to an unsigned int by using strtoul() I am getting a weird result and can't find a way to fix it. my code is as follows:

void loadData(TennisStoreType* ts, char* customerFile, char* stockFile) {

   FILE *customerdata;
   FILE *stockdata;
   const char *DEL = ",\n\t\r";
   CustomerNodeType newdata;
   int datapos = 0;
   char buffer[BUFFER_SIZE];
   char *remainder;
   char *token;
   char *cantload = "Can not load data from ";
   char *errorload = "Invalid data entry in ";
   char *canload = "Loading data from ";

   customerdata = fopen(customerFile, "r");
   stockdata = fopen(stockFile, "r");

   /* checks if the file can open*/
   if (customerdata == NULL) {

      fprintf(stderr, "%s%s\n", cantload, customerFile);

   } else {

      printf("%s%s\n", canload, customerFile);

      while(fgets(buffer, BUFFER_SIZE, customerdata) != NULL) {

         token = strtok(buffer, DEL);

         while(token != NULL) {

            /* this function checks weather the information is valid for
            the data position entered*/
            if (validCustData(datapos, token)) {

               /* Where abouts in the structure to load the data to*/
               switch (datapos) {
                  case 0:
                     strcpy(newdata.custID, token);
                     printf("successfully added %s in custID\n",     newdata.custID);
                 break;

                  case 1:
                     /*my own check to allow surnames to be entered over 12 characters
                     as in reality they do exist but it will only accept the first
                     12 characters and enter them into the structure

                     if (strlen(token) <= SURNAME_MAX) {
                        strcpy(newdata.surname, token);
                     } else {
                        strncpy(newdata.surname, token, SURNAME_MAX);
                        newdata.surname[SURNAME_MAX] = '\0';
                     }
                     printf("successfully added %s in surname\n", newdata.surname);
                     break; */


                      strcpy(newdata.surname, token);
                     printf("successfully added %s in surname\n", newdata.surname);
                     break;

                  case 2:
                     strcpy(newdata.firstName, token);
                     printf("successfully added %s in first name\n", newdata.firstName);
                     break;

                  case 3:
                     strcpy(newdata.address, token);
                     printf("successfully added %s in address\n", newdata.address);
                     break;

                  case 4:
                     strcpy(newdata.suburb, token);
                     printf("successfully added %s in suburb\n", newdata.suburb);
                     break;

                  case 5:
                     newdata.postCode = strtoul(token, &remainder, POSTCODE_LEN);
                     printf("successfully added %lu in postcode\n", newdata.postCode);
                     remainder = NULL;
                     break;

                  case 6:
                     newdata.phoneNum = strtoul(token, &remainder, PHONENUM_LEN);
                     printf("successfully added %lu in phone number\n", newdata.phoneNum);
                     remainder = NULL;
                     break;
               }

            } else {
               fprintf(stderr,"%s%s '%s'\n", errorload, customerFile, token);
               exit(EXIT_FAILURE);
               return;
            }


            if (datapos == DATA_POSITIONS) {
               datapos = 0;
            } else {
               datapos = datapos + 1;
            }

            token = strtok(NULL, DEL);
         }
      }
   }

   if (stockdata == NULL) {

      fprintf(stderr, "%s%s\n", cantload, stockFile);

   } else {

      /* to do */

      printf("%s%s\n", canload, stockFile);
   }
}

the data that is being loaded in is datapos = 5: and token = 3333: and the result that is printing is... "successfully added 255 in postcode".

as stated above, without changing the input and changing case 5: to strcpy() instead of strtoul() works but I can not do this as it does not follow the flags -pedantic -ansi and -Wall. which it needs to 100%.

ps: if needed POSTCODE_LEN is defined as 4 and I have included both stdlib.h and string.h


Solution

  • The third argument to strtoul is not the string length (the whole string us always used) - but the number base (aka radix). Try passing in 10 instead of 4.