2013-03-08 63 views
0

我想從有這樣的交流輸入文件整數:括號(我用的不是一個數字的大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; 
    } 

} 

}

+0

什麼是'temp',順便說一下?它保證'temp [1]'是0嗎? – 2013-03-08 21:51:46

+0

在進入這段代碼之前如何設置'ch'?問題可能出現在您未發佈的周圍環境中。 – Barmar 2013-03-08 21:51:57

+0

@user哪裏'processIndex'更新? – 2013-03-08 22:03:19

回答

1

首先閱讀評論):

你聲明char temp[1];它必須是一個大小根據您的代碼大小爲2(否則由於內存溢出導致未定義的行爲):

char temp[2]; 
while (isdigit(ch)) { // from `(` to `)` 
    temp[0] = ch; // should be a null terminated 
    temp[1] = '\0'; // add this step; 
    strcat(numberAppended, temp); 
    ch = fgetc(&file); 
} 

二:numberAppended是解析到類型的字符串:"0 9 500 3"
和您的呼籲與strtol

number = (int)strtol(numberAppended, &end, 0); 
            ^
             output argument 

語法:

long int strtol(const char *numberAppended, char **end, int base); 

  • numberAppended:要被轉換爲一個長整型字符串。
  • end:指向一個指針,該指針將被設置爲字符串「numberAppended」中的長整數之後的立即字符

而且你是寫的是這樣的:(閱讀評論

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

我下面的代碼將有助於你瞭解如何使用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; 
} 

輸出:

2001 
11 
223 
444 
566 

No of elements: 5 

第三:可能是它不是一個錯誤,但我找不到地方processIndex在更新之前switch(){}你的代碼..

+0

在此代碼之前有一個好於平均機會的'temp [1]'已經設置_already_。 – paxdiablo 2013-03-08 21:56:39

+0

@paxdiablo是但它不完整的代碼和你的理由我評論'應該是一個null終止'在我的代碼 – 2013-03-08 22:00:11

+0

爲** OP **讓我知道你是否需要更多的幫助..也有文件閱讀錯誤! – 2013-03-08 22:47:30

相關問題