无知,c - 找不到这不起作用的原因 - printf

clueless, c - cannot find the reason why this isn't working - printf

过去几个小时我一直在处理这个问题,我似乎无法找到它为什么不起作用,store_car_stats 函数中的 printf 从我的 argv 中打印出奇怪的东西并且它运行在 gdb 上,但 seg 故障正常。这真的很奇怪,我似乎找不到问题。

它需要的输入文件是

3
1993 Chevy Blazer 100200
2007 Honda Civic 23787
1903 Ford ModelT 90000 

更新文件是

4
1993 Chevy Blazer 39
1903 Ford ModelT 20
1993 Chevy Blazer 2
2014 Jeep Patriot 100

我的代码

#include <stdio.h>
#include <stdlib.h>
#define STRLEN 20

typedef struct automobiles{
        int year;
        char* make;
        char* model;
        int miles;
}Car;

void fill_garage(Car** carage,int*size,char* inputFile);
int equals(Car* garage,int year,char* make,char* model);
void drive_cars(Car* garage,int* num_cars,char* driving_records);
void store_car_stats(Car* garage,int num_cars,char* outFile);
void empty_garage(Car* garage,int num_cars);

int main(int argc,char* argv[]){
    if(argc!=4){
        printf("Invalid number of arguments, You have %d",argc);
        return 0;
    }
    int size;
    int newsize;
    Car* mywhips;

    fill_garage(&mywhips,&size,argv[1]);
    printf("\n ))) %d %s %s %d",mywhips[1].year,mywhips[1].make,mywhips[1].model,mywhips[1].miles);
    drive_cars(mywhips,&newsize,argv[2]);

    store_car_stats(mywhips,newsize,argv[3]);
    empty_garage(mywhips,newsize);

    return 0;
}

void fill_garage(Car** warehouse,int*size,char*inputFile){
    FILE* file=fopen(inputFile,"r");

    int i;
    Car* garage;

    fscanf(file,"%d",size); //get size and place in address
    if(file==NULL){//did file open correctly?
        printf("File returned NULL when attempting to read..try again");
    }
    garage=malloc(sizeof(Car)*(*size)); //reserve memory for the file input
    warehouse = &garage;
    printf("input File DATA \n");
    for(i=0;i<*size;i++){
        garage[i].make = malloc(sizeof(char)*STRLEN); //reserve memory for the make
        garage[i].model = malloc(sizeof(char)*STRLEN); //and model
        //scan input and place in struct
        fscanf(file,"%d %s %s %d",&garage[i].year,garage[i].make,garage[i].model,&garage[i].miles); //seg fault here
        printf("\nYear: %d \nMake: %s\nModel: %s\nMiles: %d\n",garage[i].year,garage[i].make,garage[i].model,garage[i].miles); //seg fault here
    }
    fclose(file);
    //printf("here");
    return;

}

int equals(Car* car,int year,char* make,char* model){
    if(year == car->year){
        if(make == car->make){
            if(model == car->model){
                return 1;
            }
        }
    }
    return 0;
}

void drive_cars(Car* garage,int* num_cars,char* driving_records){
    FILE* file=fopen(driving_records,"r");
    int year,miles,i;
    char* make;
    char* model;

        if(file == NULL){
            printf("update file opened NULL");
        }

        fscanf(file,"%d",num_cars);

        for(i=0;i<*num_cars;i++){
            make = malloc(sizeof(char)*(*num_cars));
            model = malloc(sizeof(char)*(*num_cars));
            fscanf(file,"%d%s%s%d",&year,make,model,&miles);
            printf("\nYear: %d \nMake: %sModel: %s\nMiles: %d\n",year,make,model,miles);
            if(equals(&(garage[i]),year,make,model)==1){
                printf("here");
                garage[i].miles+=miles;
            }
        }
        //printf("here");
        free(make);
        free(model);

}

void store_car_stats(Car* garage, int num_cars,char* outFile){
    FILE* file=fopen(outFile,"w");
        //printf("store_car_stats\n");
        if(file == NULL){
            printf("ouptput file returned NULL");
            return;
        }
    int i;
    printf("%d",num_cars);
    for(i=0;i<num_cars;i++){
        printf("\nYour %d %s %s has now driven %d", garage[i].year, garage[i].make, garage[i].model, garage[i].miles);
        fprintf(file,"\n\n\nA %d %s %s that has now driven %d",garage[i].year,garage[i].make,garage[i].model,garage[i].miles);
    }
    fclose(file);

}

void empty_garage(Car* garage, int num_cars){

    int i;

    for(i=0;i<num_cars;i++){
        //free(garage[i].make);
        //free(garage[i].model);
    }
    //free(garage);
}

fill_garage 中,这是一个问题:

warehouse = &garage;

garage 是一个局部变量,而您正在将局部变量的地址分配给 warehouse。这是错误的,不管函数中的其余代码如何。当函数 returns 时,那个 garage 变量不复存在,因此 &garage 将指向谁知道什么。

您可能打算这样做:

*warehouse = garage;