2015-02-17 44 views
1

我一直在爲此工作最近幾個小時,我似乎無法找到它爲什麼不工作,print_f在store_car_stats函數從我的argv打印奇怪的東西,它通過gdb運行,​​但seg故障正常。它真的很奇怪,我似乎無法找到問題。clueless,c - 找不到原因,這是行不通的 - printf

它需要輸入文件

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); 
} 
+1

你分享什麼「怪異」的數據,你看,哪些是你期待什麼呢? – 2015-02-17 21:10:49

+1

「過去幾個小時我一直在爲此工作」 - ITYM是最近幾天,而且您的代碼中仍然存在很多在之前的問題中指出的錯誤。 – 2015-02-17 22:40:04

回答

2

fill_garage,這是一個問題:

warehouse = &garage;

​​是一個局部變量,並且您正在將局部變量的地址分配給warehouse。這是錯誤的,無論函數中的其他代碼如何。當函數返回時,​​變量不再存在,所以&garage將指向誰知道什麼。

你可能打算這樣做:

*warehouse = garage;

+0

這工作!然而,有幾個更容易修復的錯誤,但這是影響我輸出的主要錯誤,謝謝(我希望我可以upvote)。 – 2015-02-17 23:17:58

相關問題