简体   繁体   中英

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

I've been working on this for the last few hours and I cannot seem to find why it isn't working, the printf in the store_car_stats function prints weird stuff from my argv and it runs through on gdb but seg faults normally. its really weird and I cannot seem to find the problem.

the input file it requires is

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

the update file is

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

my code

#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);
}

In fill_garage , this is an issue:

warehouse = &garage;

The garage is a local variable, and you're assinging the address of a local variable to warehouse . This is wrong, regardless of the rest of the code in the function. When the function returns, that garage variable ceases to exist, so the &garage will be pointing to who-knows-what.

You probably meant to do this:

*warehouse = garage;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM