gpt4 book ai didi

c - token to unsigned printing odd 值

转载 作者:太空宇宙 更新时间:2023-11-04 08:30:35 24 4
gpt4 key购买 nike

正如标题所暗示的那样,我从 strtoul 得到了一些奇怪的返回,我已经通过使用 strcpy 将数据存储为字符串进行了测试,它给出了正确的值,但是一旦我尝试将其更改为无符号的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 的情况下:使用 strcpy() 而不是 strtoul() 是可行的,但我不能这样做,因为它不遵循标志 -pedantic -ansi 和 -Wall。它需要 100%。

ps:如果需要,POSTCODE_LEN 定义为 4,我已经包含了 stdlib.h 和 string.h

最佳答案

strtoul 的第三个参数不是字符串长度(我们总是使用整个字符串)——而是数字基数(也称为基数)。尝试传入 10 而不是 4。

关于c - token to unsigned printing odd 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28537022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com