2010-11-15 298 views
2

我要在幾個小時內提交這個assigment,我很緊張, 它是一種加油站管理程序,handeling輸入文件和打印結果... 它只有1個.c文件,這就是我的第一行代碼,它定義了結構新的C,錯誤C2371:'錯誤':重新定義;不同的基本類型

#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(char *orders) { 
FILE *input , *output; 
input = fopen(orders, "rt"); 
output = fopen("result.txt" , "wt"); 
if (input == NULL) { 
    error("can't open file, might not exists"); 
} 
else if (output == NULL) { 
    error("can't open file"); 
} 
else { 
    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); 
} 

static void error(char *msg) { 
fprintf(stderr , "Error: %s\n", msg); 
exit(1); 
} 

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

} 

現在的錯誤指向static void error(char *msg)函數但在此之前它指向void CommandsSwitch(char *orders)CommandsSwitch給出相同的錯誤。

plz試圖幫助和指導我,我很困惑。 tnx。

+0

您可能缺少一個符號(分號,例如)的地方。這些事情會在意想不到的地方造成怪異的錯誤。 – casablanca 2010-11-15 17:29:35

+0

好的......但現在它是運行時錯誤,並且似乎fopen的調用干擾它,導致調用堆棧指的是..什麼是從argv []獲取文件指針的正確方法? – 2010-11-15 18:19:16

回答

4

你的一個問題是你在CommandSwitcherror功能的使用。

void CommandsSwitch(char *orders) { 
FILE *input , *output; 
input = fopen(orders, "rt"); 
output = fopen("result.txt" , "wt"); 
if (input == NULL) { 
    error("can't open file, might not exists"); 
} 
else if (output == NULL) { 
    error("can't open file"); 
} 
/* ...more... */ 

您在error功能的實際申報前使用此error功能進一步下跌:

static void error(char *msg) { 
fprintf(stderr , "Error: %s\n", msg); 
exit(1); 
} 

你跑進C的隱函數聲明功能,它允許您使用功能,如果它是隱式聲明的,因爲你不使用函數原型。

的編譯器,它的作用就好像有聲明爲

int error(...); 

的函數,而與功能的衝突:

static void error(char *); 

所以基本上,代碼充當雖然已經有一個名爲error的函數聲明,默認返回類型爲int。然後編譯器會碰到你的函數聲明,並且抱怨函數error被重新定義。

解決此問題的最簡單方法至少是在void CommandsSwitch之前移動error函數。

你將要讀到函數聲明和原型:

+0

好吧......但現在它是運行時錯誤,而且似乎fopen的調用幹了它,導致調用堆棧指的是..什麼是正確的方法從argv []獲取文件指針? – 2010-11-15 18:23:50

+0

@Kobby:我不確定你是否可以不接受我的答案,但請這樣做,並用你的新錯誤更新你的問題。我試圖解決你的直接問題,但我確信會有更多錯誤需要遵循。 – birryree 2010-11-15 18:29:00

1

這對你的編譯時錯誤沒有幫助,但是當你真正嘗試運行這段代碼時,你應該意識到你沒有爲ptemp分配任何內存 - 它只是一個dangling pointer。將定義改爲:

char *ptemp, *pfuncNum, *pcarID, *pstationName; 

例如

char ptemp[LINE_MAX]; 
char *pfuncNum, *pcarID, *pstationName; 
相關問題