我正在尝试从具有如下输入的 ac 文件中获取整数:
(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;
}
}
}