C 为什么atoi和strtol大多数时候只返回字符串中的第一个数字?
我试图从具有如下输入的c文件中获取int:C 为什么atoi和strtol大多数时候只返回字符串中的第一个数字?,c,atoi,strtol,C,Atoi,Strtol,我试图从具有如下输入的c文件中获取int: (0 3 200 3) (0 9 500 3) (98 20 500 3) (100 1 100 3) (100 100 500 3) atoi和s对于括号后面的第一个数字(我使用while循环和大于一位的strcat数字)和任何只有一位的数字都可以很好地工作,但是它们只返回括号后面不正确的数字的第一位数字 下面是该方法的代码: void allProcesses(FILE file, struct process processArray[])
(0 3 200 3) (0 9 500 3) (98 20 500 3) (100 1 100 3) (100 100 500 3)
atoi和s对于括号后面的第一个数字(我使用while循环和大于一位的strcat数字)和任何只有一位的数字都可以很好地工作,但是它们只返回括号后面不正确的数字的第一位数字
下面是该方法的代码:
void allProcesses(FILE file, struct process processArray[]) {
char ch;
int number;
int a, b, c, io;
int arrayCount = 0;
int processIndex = 0;
char temp[1];
while ((ch = fgetc(&file)) != EOF) {
if (isdigit(ch)) {
char numberAppended[20] = "";
while (isdigit(ch)) {
temp[0] = ch;
strcat(numberAppended, temp);
ch = fgetc(&file);
}
char* end;
number = (int)strtol(numberAppended, &end, 0);
printf("The number is %d\n",number);
int atoinum = atoi(numberAppended);
switch (processIndex) {
case 0:
a = number;
if (DEBUG == TRUE) {
printf("a = %c\n", a);
printf("NUmber a is %d\n", a);
}
processIndex++;
break;
case 1:
b = number;
if (DEBUG == TRUE) {
printf("b = %c\n", b);
printf("NUmber b is %d\n", b);
}
processIndex++;
break;
case 2:
c = number;
if (DEBUG == TRUE) {
printf("c = %c\n", c);
printf("NUmber c is %d\n", c);
}
processIndex++;
break;
case 3:
io = number;
if (DEBUG == TRUE) {
printf("io = %c\n", io);
printf("NUmber io is %d\n", io);
}
processIndex++;
break;
default:
break;
}
}
if (ch == ')') {
processArray[arrayCount] = makeProcess(a, b, c, io);
arrayCount++;
processIndex = 0;
}
}
}首先(阅读评论):
您已声明char temp[1]代码>一个大小根据您的代码,它的大小必须为2
(否则,由于内存溢出,行为未定义):
Second:您的numberAppended
被解析为一种字符串:“09500 3”
你的电话呢
number = (int)strtol(numberAppended, &end, 0);
^
output argument
strtol的语法:
long int strtol(const char *numberAppended, char **end, int base);
其中
numberAppended
:要转换为长整数的字符串李>
end
:指向一个指针,该指针将被设置为字符串“numberAppended
”中长整数后面的字符李>
你可以这样写:(阅读评论)
下面的代码将帮助您了解如何使用strtol()
从numberAppended
字符串中解析和提取数字:
#include <stdio.h> /* printf */
#include <stdlib.h> /* strtol */
int main (){
char numberAppended[] = "2001 11 223 444 566";
char * end;
long int li;
end =numberAppended;
int base =10;
int ele = 0;
while(li=strtol (end, &end, base)){
printf("%ld \n", li);
ele += 1;
}
printf("\nNo of elements: %d", ele);
return 0;
}
第三:可能不是错误,但在switch(){}
之前,我找不到processIndex
在您的代码中更新的位置。顺便问一下,什么是temp
?是否保证temp[1]
为0?进入此代码之前如何设置ch
?问题可能在周围的循环中,您没有发布。@user WhereprocessIndex
updates?char temp[1]代码>制造<代码>strcat(数字等待,温度)代码>在temp[0]=ch之后未定义的行为
@danieldischer yes未定义其行为在此代码之前已经设置了一个好于平均值的temp[1]
。@paxdiablo yes但它的代码不完整,出于您的原因,我在mycodefor OP中注释的应该是以null结尾的
,如果您需要更多帮助,请告诉我。。还有文件读取错误!非常感谢。我忽略了这个问题,没有意识到它有那么简单。
end = numberAppended; // assign to first string
// in a loop {
number = (int)strtol(end, &end, 0); // in loop end is input &end is output
printf("The number is %d\n",number);
//}
#include <stdio.h> /* printf */
#include <stdlib.h> /* strtol */
int main (){
char numberAppended[] = "2001 11 223 444 566";
char * end;
long int li;
end =numberAppended;
int base =10;
int ele = 0;
while(li=strtol (end, &end, base)){
printf("%ld \n", li);
ele += 1;
}
printf("\nNo of elements: %d", ele);
return 0;
}
2001
11
223
444
566
No of elements: 5