0

タイトルが示唆しているように、strtoul から奇妙な戻り値が得られているため、strcpy を使用してデータを文字列として格納するだけでテストしましたが、正しい値が得られましたが、使用して unsigned int に変更しようとするとすぐにstrtoul() 奇妙な結果が得られ、それを修正する方法が見つかりません。私のコードは次のとおりです。

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

ロードされているデータは datapos = 5: および token = 3333: であり、印刷される結果は... 「郵便番号に 255 が正常に追加されました」です。

上記のように、入力を変更せず、ケース 5: を strtoul() の代わりに strcpy() に変更しなくても機能しますが、フラグ -pedantic -ansi および -Wall に従っていないため、これを行うことはできません。100%必要です。

ps: 必要に応じて POSTCODE_LEN は 4 として定義され、stdlib.h と string.h の両方を含めました。

4

1 に答える 1

2

strtoul の 3 番目の引数は、文字列の長さ (常に使用される文字列全体) ではなく、基数 (基数) です。4 の代わりに 10 を渡してみてください。

于 2015-02-16T08:29:01.827 に答える