我有以下输入文件:
WO98EKOYMPCAUEWT0 Honda Civic 2011 4
7W32UAERZFBCB3S6P Chevrolet Tahoe 2011 6
DNU7XQO8LLA9I6YFX Toyota Tercel 2012 4
DNU7XQO8LLA9I6YFX Toyota Tercel 2012 4
DNU7XQO8LLA9I6YFX Toyota Tercel 2012 4
7W32UAERZFBCB3S6P Chevrolet Tahoe 2011 6
这实际上是我们交给我们的一项任务,我已经挣扎了几天没有结果。
读取每一行并将第二个、第三个和第四个的值放入一个结构中。
之后添加一个新列,它是基于上面 3 列的出现次数。
按第二,第三和第四排序数据。然后将它们输出到一个文件中。
到目前为止,这是我的代码:
struct Car
{
char *CarMake;
char *CarModel;
char *CarMakeYear;
int Occurances;
};
int main(int argc, char *argv[])
{
//File related
FILE *inputFile, *outputFile;
char fileName[] = "";
//Struct related
int carCounter = 0;
struct Car cars[50];
struct Car car;
car.CarMake = (char*)malloc( 200 *sizeof(char));
car.CarMakeYear = (char*)malloc( 200 *sizeof(char));
car.CarModel = (char*)malloc( 200 *sizeof(char));
car.Occurances = 0;
printf("Please enter file name:", fileName);
scanf("%s",fileName);
inputFile = fopen(fileName,"r");
while(fscanf(inputFile, "%*s %s %s %s %*s\n",car.CarMake, car.CarModel,car.CarMakeYear ) != EOF)
{
cars[carCounter].CarMake = car.CarMake;
carCounter++;
}
fclose(inputFile);
int i=0;
for(i=0;i<6;i++)
printf("%s %s %s \n", cars[i].CarMake, cars[i].CarModel,cars[i].CarMakeYear);
system("PAUSE");
return 0;
}
上面的 printf 将采用最后一个数组值并继续打印,这意味着它不起作用。任何人都请帮忙,我要到午夜之前才能提交。