c程序在调用函数时崩溃

时间:2018-05-16 11:52:29

标签: c function crash

我正在尝试创建一个管理汽车修理记录的程序,一切正常,直到case 6此代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
    int day;
    int month;
    int year;
}date;
typedef struct{
    char pie_name[50];
    int pie_num;
    float pie_price;
}pie_st;
typedef struct{
    long car_num;
    char car_type[20];
    char car_colour[20];
    char owner_name[50];
    long phone_num;
}own_cars_st;
typedef struct{
    int management_num;
    long car_num;
    date date_of_management;
    int pieces_num;
    pie_st pie_rec[1000];
    float management_fees;
    float total_price;
}management_st;
void check_file(FILE *ptf);
void add_car_rec();
void add_management_rec();
int delete_car_rec(long search);
int search_own(char owner[]);
int car_man(long search);
int main(){
    int option,c=0,found=1;
    long search;
    char owner[50];
    while(c==0)
    {
        printf("\nplease enter your choice:\n");
        printf("1.add new car record\n");
        printf("2.add new car management record\n");
        printf("3.delete a car record by number\n");
        printf("4.view all car numbers for a specific owner\n");
        printf("5.display all management records\n");
        printf("6.view a specific car's management records and the total cost\n");
        printf("7.view the most used gear piece's number\n");
        printf("8.view a list of all cars that had management after a specific date\n");
        printf("9.view all cars with changed tires\n");
        printf("10.exit\n");
        scanf("%d",&option);
        switch(option)
        {
            case 1:
                add_car_rec();
                break;
            case 2:
                add_management_rec();
                break;
            case 3:
                printf("please enter the number of the car you want to delete its number:\t");
                scanf("%ld",&search);
                found=delete_car_rec(search);
                if(found==1)
                    printf("there's no record available for this number!\n");
                else
                    printf("record has been deleted!\n");
                break;
            case 4:
                printf("please enter the name of the owner:\t");
                fflush(stdin);
                scanf("%[^\n]s",owner);
                found=search_own(owner);
                if(found==1)
                    printf("owner was not found!\n");
                break;
            case 5:
                break;
            case 6:
                printf("please enter the number of the car you want to view its management records:\t");
                scanf("%ld",&search);
                printf("7");
                car_man(search);
                printf("8");
                if(found==1)
                    printf("this car number has no management records!\n");
                break;
            case 7:
                break;
            case 8:
                break;
            case 9:
                break;
            case 10:
                c=10;
                break;
            default:
                printf("please choose one of the available options\n");
        }
    }
    return 0;
}
void add_car_rec(){
    own_cars_st new_car,old_cars[100];
    printf("please enter the car information:\n");
    printf("car number:\t");
    scanf("%ld",&new_car.car_num);
    printf("car type:\t");
    fflush(stdin);
    scanf("%[^\n]s",new_car.car_type);
    printf("car colour:\t");
    fflush(stdin);
    scanf("%[^\n]s",new_car.car_colour);
    printf("owner name:\t");
    fflush(stdin);
    scanf("%[^\n]s",new_car.owner_name);
    printf("phone number:\t");
    scanf("%ld",&new_car.phone_num);
    FILE *ptf;
    int n=0,i,j,fc;
    ptf=fopen("owners and cars.bin","ab+");
    if(ptf!=NULL){
        fseek(ptf,0,2);
        fc=ftell(ptf);
        rewind(ptf);
        if(fc==0)
        {
            fwrite(&new_car,sizeof(own_cars_st),1,ptf);
            fclose(ptf);
        }
        else{
            while(1==fread(&old_cars[n],sizeof(own_cars_st),1,ptf))
            {
                n++;
            }
            for(i=0;i<n+1;i++)
            {
                if(new_car.car_num<old_cars[i].car_num)
                {
                    for(j=n+1;j>i;j--)
                    {
                        old_cars[j]=old_cars[j-1];
                    }
                    break;
                }
            }
            old_cars[i]=new_car;
            fclose(ptf);
            ptf=fopen("owners and cars.bin","wb");
            check(ptf);
            for(i=0;i<n+1;i++)
            {
                fwrite(&old_cars[i],sizeof(own_cars_st),1,ptf);
            }
            fclose(ptf);
        }
    }
}
void add_management_rec(){
    FILE *ptf;
    int i,count=0,empty;
    float sum=0;
    management_st new_manage_rec,last_manage_rec;
    printf("please enter the new management record information:\n");
    printf("fixed car's number:\t");
    scanf("%ld",&new_manage_rec.car_num);
    printf("date of management:\n");
    printf("day:\t");
    scanf("%d",&new_manage_rec.date_of_management.day);
    printf("month:\t");
    scanf("%d",&new_manage_rec.date_of_management.month);
    printf("year:\t");
    scanf("%d",&new_manage_rec.date_of_management.year);
    printf("the number of the used pieces types:\t");
    scanf("%d",&new_manage_rec.pieces_num);
    printf("please enter each type's information:\n");
    for(i=0;i<new_manage_rec.pieces_num;i++)
    {
        printf("piece #%d\n",i+1);
        printf("piece name:\t");
        scanf("%s",new_manage_rec.pie_rec[i].pie_name);
        printf("number of used pieces:\t");
        scanf("%d",&new_manage_rec.pie_rec[i].pie_num);
        printf("piece's price:\t");
        scanf("%f",&new_manage_rec.pie_rec[i].pie_price);
        sum+=new_manage_rec.pie_rec[i].pie_price*(float)new_manage_rec.pie_rec[i].pie_num;
    }
    printf("management fees:\t");
    scanf("%f",&new_manage_rec.management_fees);
    new_manage_rec.total_price=new_manage_rec.management_fees+sum;
    ptf=fopen("management.bin","ab+");
    if(ptf!=NULL)
    {
        fseek(ptf,0,2);
        empty=ftell(ptf);
        rewind(ptf);
        if(empty==0){
            new_manage_rec.management_num=1;
            fwrite(&new_manage_rec,sizeof(management_st),1,ptf);}
        else
        {
            fseek(ptf,-sizeof(management_st),2);
            fread(&last_manage_rec,sizeof(management_st),1,ptf);
            new_manage_rec.management_num=last_manage_rec.management_num+1;
            fwrite(&new_manage_rec,sizeof(management_st),1,ptf);
        }
    }
    fclose(ptf);
}
void check(FILE *ptf){
    if(ptf==NULL)
    {
        printf("error!\n");
        exit -1;
    }
}
int delete_car_rec(long search){
    FILE *ptf;
    int count=0,i,j,f=1;
    own_cars_st avai_cars[100];
    ptf=fopen("owners and cars.bin","rb");
    check(ptf);
    while(fread(&avai_cars[count],sizeof(own_cars_st),1,ptf)==1)
    {
        count++;
    }
    fclose(ptf);
    if(count==1)
    {
        if(search==avai_cars[0].car_num)
        {
            f=0;
            ptf=fopen("owners and cars.bin","wb");
            fclose(ptf);
        }
    }
    else
    {
        for(i=0;i<count;i++)
        {
            if(search==avai_cars[i].car_num)
            {
                f=0;
                for(j=i+1;j<count;j++)
                {
                    avai_cars[i]=avai_cars[j];
                }
            }
        }
        ptf=fopen("owners and cars.bin","wb");
        check(ptf);
        for(i=0;i<count;i++)
            fwrite(&avai_cars[i],sizeof(own_cars_st),1,ptf);
        fclose(ptf);
    }
    return f;
}
int search_own(char owner[]){
    FILE *ptf;
    int found=1,count=0,i;
    own_cars_st avai_cars[100];
    ptf=fopen("owners and cars.bin","rb");
    check(ptf);
    while(1==fread(&avai_cars[count],sizeof(own_cars_st),1,ptf))
        count++;
    fclose(ptf);
    for(i=0;i<count;i++)
    {
        if(strcmp(avai_cars[i].owner_name,owner)==0)
        {
            found=0;
            printf("car#%d:\t%ld\n",i+1,avai_cars[i].car_num);
        }
    }
    return found;
}
int car_man(long search){
    printf("*\n");
    FILE *ptf;
    management_st avai_man[100];
    int i,count=0,found=1,j;
    float total_cost=0;
    printf("0\n");
    ptf=fopen("management.bin","rb");
    check(ptf);
    printf("1\n");
    while(1==fread(&avai_man[count],sizeof(management_st),1,ptf))
        count++;
    fclose(ptf);
    printf("2\n");
    for(i=0;i<count;i++)
    {
        if(search==avai_man[i].car_num)
        {
            found=0;
            printf("date of management:\n");
            printf("day:\t");
            printf("%d",avai_man[i].date_of_management.day);
            printf("month:\t");
            printf("%d",avai_man[i].date_of_management.month);
            printf("year:\t");
            printf("%d",avai_man[i].date_of_management.year);
            printf("the number of the used pieces types:\t");
            printf("%d",avai_man[i].pieces_num);
            printf("please enter each type's information:\n");
            for(j=0;j<avai_man[i].pieces_num;j++)
            {
                printf("piece #%d\n",i+1);
                printf("piece name:\t");
                printf("%s",avai_man[i].pie_rec[j].pie_name);
                printf("number of used pieces:\t");
                printf("%d",avai_man[i].pie_rec[j].pie_num);
                printf("piece's price:\t");
                printf("%f",avai_man[i].pie_rec[j].pie_price);
            }
            printf("management fees:\t");
            printf("%f",avai_man[i].management_fees);
        }
        total_cost+=avai_man[i].management_fees;
    }
    printf("3\n");
    printf("total cost for this car:\t%f",total_cost);
    return found;
}

程序似乎在调用函数car_man时崩溃,我写了几个printf来追踪程序崩溃时它只到达printf(&#34; 7 \ n&#34;)。 任何帮助将不胜感激^。^ 附:编译器中没有任何警告或任何东西

1 个答案:

答案 0 :(得分:1)

management_st avai_man[1000];

这是一个在堆栈上创建的非常大的数组;你可能正在耗尽堆栈空间。

考虑将数组移动到动态分配的内存,或使其成为静态。