2

我必须在几个小时内提交这个分配,我很紧张,它有点像加油站管理程序、处理输入文件和打印结果......它只有 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(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相同的错误。

请尝试帮助和指导我,我很困惑。tnx。

4

2 回答 2

4

您的问题之一是您error使用CommandSwitch.

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. 然后编译器会遇到你的void error()函数声明,并抱怨重新定义了 function error

至少,解决此问题的最简单方法是将error函数移到void CommandsSwitch.

您将需要阅读有关函数声明和原型的信息:

于 2010-11-15T17:30:58.547 回答
1

这对您的编译时错误没有帮助,但是当您实际尝试运行此代码时,您应该知道您没有为 ptemp 分配任何内存 - 它只是一个悬空指针。更改定义:

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

例如

char ptemp[LINE_MAX];
char *pfuncNum, *pcarID, *pstationName;
于 2010-11-15T17:26:01.090 回答