gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:10:47 26 4
gpt4 key购买 nike

过去几个小时我一直在研究这个,我似乎无法找到它为什么不工作,store_car_stats 函数中的 printf 从我的 argv 打印奇怪的东西,它在 gdb 上运行,但通常会出现段错误.这真的很奇怪,我似乎找不到问题所在。

它需要的输入文件是

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 中,这是一个问题:

仓库 = &garage;

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

您可能打算这样做:

*仓库 = 车库;

关于无知,c - 找不到这不起作用的原因 - printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28570995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com