2010-11-15 131 views
-1

我沒有編譯錯誤,但它崩潰上運行時, 這是我的相關代碼,首先它的結構:C指針,我做錯了什麼?

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

struct Gas_Station *pgasStationHead = NULL; 
typedef struct Gas_Station { 
    char *name; 
    double octan95SS; 
double octan95FS; 
double octan98SS; 
double octan98FS; 
double gasSoldTotal; 
double gasSoldSS; 
double gasSoldFS; 
struct Gas_Station *pgasStationNext; 
struct Client_List *pclientHead; 
} Station; 

typedef struct Client_List { 
    char carID[10]; 
char gasType[3]; 
    double gasAmount; 
char serviceType[12]; 
struct Client_List *pclientNext; 
} Client; 

,之後有問題的區域:

void CommandsSwitch(FILE *input , FILE *output) { 

    do { 
    int i; 
    char *ptemp , *pfuncNum, *pcarID, *pstationName; 
    ptemp = fgets(ptemp , 80 , input); 
    if (ptemp[0] != '#') { 
    pfuncNum = strtok(ptemp , ","); 
    i = (int)pfuncNum[0]; 
    switch (i) 
    { 
    case 1: 
    HowMuchGasPerStation(output); 
    break; 

    case 2 : 
    pstationName = strtok(pstationName , ","); 
    AverageGasInSpecieficStation(output , pstationName); 
    break; 

    case 3 : 
    HowMuchGasInAllStations(output); 
    break; 

    case 4 : 
    HowMuchGasFSInAllStations(output); 
    break; 

    case 5 : 
    pcarID = strtok(ptemp , ","); 
    HowMuchGasSoldByCarID(output , pcarID); 
    break; 
    case 6 : 
    pcarID = strtok(ptemp , ","); 
    pstationName = strtok(pstationName , ","); 
    HowMuchGasSoldByStationPerCarID(output , pcarID , pstationName); 
    break; 
    case 7 : 
    pcarID = strtok(ptemp , ","); 
    StationsWithClientByCarID(output , pcarID); 
    break; 
    case 8 : 
    pcarID = strtok(ptemp , ","); 
    pstationName = strtok(pstationName , ","); 
    HowMuchClientSpentByStation(output , pcarID , pstationName); 
    break; 
    case 9 : 
    pcarID = strtok(ptemp , ","); 
    HowMuchClientSpentInTotalByCarID(output , pcarID); 
    break; 

    case 10 : 
    pstationName = strtok(pstationName , ","); 
    ClientDetailsBySpecieficStation(output , pstationName); 
    break; 
    } 
    } 
    }while(!feof(input)); 

fclose(input); 
fclose(output); 
} 

int main (int argc, char* argv[]) { 
int i; 
FILE *f , *input , *output; 
for (i = 2; i < argc; i++) { 
    f = fopen(argv[i] , "r"); 
    if (f == NULL) { 
    error("can't open file, might not exists"); 
    } 
    else { 
    AddStation(f); 
    fclose(f); 
    } 

} 
if (argv[1] != NULL) { 
    input = fopen(argv[1] , "r"); 
    if (input == NULL) { 
    error("can't open file, might not exists"); 
    } 
} 

output = fopen("result.txt" , "w"); 
if (output == NULL) { 
    error("can't open file"); 
} 
CommandsSwitch(input , output); 

return 0; 
}` 
在CommandSwitch

函數調用堆棧指向* ptemp,說我不能使用它,因爲它沒有初始化或什麼... 我做錯了什麼?

+2

請格式化你的問題,我無法理解的事情,因此,不能盡力幫助你。 – Juan 2010-11-15 21:40:31

+0

有人可以編輯帖子嗎?謝謝。 – 2010-11-15 21:41:48

+0

你將首先需要一個調試器 - 查明它在哪裏崩潰。 – 2010-11-15 21:42:11

回答

7

您的ptemp變量是一個未初始化的指針。 改爲使用malloc分配適當的空間或將其定義爲數組。

4

您必須在fgets之前爲ptemp分配內存。

你可以做到這一點無論是動態還是在棧上:

  • char ptemp[100];
  • char* ptemp = (char*)malloc(100);
0

char ptemp[80];的與fgets()之前能夠幫助錯誤。或者在fgets()之前做ptemp = (char *)malloc(80*sizeof(*ptemp));

2

這是一個問題。

char *ptemp; 
ptemp = fgets(ptemp , 80 , input); 

你告訴編譯器ptemp是指向一些字符,但你永遠不分配的空間來存儲一些字符寫進去。也許這應該是:

char ptemp[80]; 
fgets(ptemp, sizeof(ptemp), input); 
0

一旦你完成處理ptemp是未初始化的指針,你可能會想改變這個部分還有:

pfuncNum = strtok(ptemp , ","); 
i = (int)pfuncNum[0]; 
switch (i) 
{ 
case 1: 
// more cases up to `10` elided. 

現在,你查看第一個字符的字符值,所以要獲得1,用戶必須輸入Ctrl + A,對於2 Ctrl + B等等。獲取10會特別有問題,因爲這是一個line-feed字符。

我的猜測是,你想要的東西,如:

i = atoi(pfuncNum); 
switch (i) { 
// ... 

這將讓用戶真正進入數字12等,在命令。 對於實際使用,您可能希望用strtol之類的東西替換atoi(儘管它提高了處理不良輸入的能力等)。

一旦你解決了這個問題,你還想再看看循環的基本結構。幾乎所有的循環形式:

do { 
    /* ... */ 
} while (!feof(input)); 

...幾乎保證它不會正常工作(通常會處理的最後一個輸入兩次)。由於您使用fgets讀取字符串,你可能想使用這樣的事情,而不是:

while (fgets(...)) { 
    /* ... */ 
};