我想從有這樣的交流輸入文件整數:括號(我用的不是一個數字的大while循環和strcat的號碼後爲什麼atoi和strtol大部分時間只會從我的字符串中返回第一個數字?
(0 3 200 3) (0 9 500 3) (98 20 500 3) (100 1 100 3) (100 100 500 3)
的atoi和S做工精細的第一個數字)和任何只有一位數字的數字,但他們只返回括號後不正確數字的第一位數字。
以下是該方法的代碼:
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;
}
}
}
什麼是'temp',順便說一下?它保證'temp [1]'是0嗎? – 2013-03-08 21:51:46
在進入這段代碼之前如何設置'ch'?問題可能出現在您未發佈的周圍環境中。 – Barmar 2013-03-08 21:51:57
@user哪裏'processIndex'更新? – 2013-03-08 22:03:19