从文本文件中读取以填充结构类型

时间:2017-04-28 08:29:40

标签: c file struct

[代码现在正在运行,感谢您的帮助。]
我可以让程序打印第一组struct auto_t。当我尝试打印其他设置没有任何反应或我收到错误。

这就是我要做的。
定义结构类型auto_t以表示汽车。包括组件 制造和模型(字符串),里程表读数,制造 和购买日期(使用另一个名为date_t的用户定义类型), 和油箱(使用用户定义类型tank_t与油箱组件 容量和当前燃料水平,以加仑计算。写I / O功能 scan_datescan_tankscan_autoprint_dateprint_tankprint_auto,并编写一个反复填充和显示的驱动程序函数 自动结构变量,直到输入文件中遇到EOF

以下是一个小数据集:

Mercury Sable 99842 1 18 2001 5 30 1991 16 12.5
Mazda Navajo 123961 2 20 1993 6 15 1993 19.3 16.7

以下代码有效:
[如果你发现我错过的代码有任何问题我不介意反馈]

#include <stdio.h>
#define SIZE 20
// the structures
typedef struct       //struct for date
{
        int month,day,year;
} date_t;

typedef struct      //struct for the tank info
{
        double capacity;
        double curent_Fuel;
} tank_t;

typedef struct      //the struct for the automobie
{
        char make[SIZE];
        char model[SIZE];
        int odometer;
        date_t manufact;
        date_t purchase;
        tank_t tank;
} auto_t;

//the function 
void print_date(date_t da);
void print_tank(tank_t ta);
void print_auto(auto_t au);
int scan_date(date_t *date);
int scan_tank(tank_t *tank);
int scan_automobile(auto_t *automo);

    //Start of program
    int main (void)
{
            auto_t car;
            int stat = 1;

            FILE *Car_data;                   //file used
            Car_data = fopen("car.txt", "r");// has the date for the cars like make, model ect.
            if (Car_data==NULL){
               printf("ERROR: File failed to open");
               getch();
               exit(1);
               fclose(Car_data);}
            else
                while(stat>0)
           {                
                stat=fscanf(Car_data, "%s %s %d %d %d %d %d %d %d %lf %lf", &car.make,
                                                                            &car.model,
                                                                            &car.odometer,
                                                                            &car.manufact.month,
                                                                            &car.manufact.day,
                                                                            &car.manufact.year,
                                                                            &car.purchase.month,
                                                                            &car.purchase.day,
                                                                            &car.purchase.year,
                                                                            &car.tank.capacity,
                                                                            &car.tank.curent_Fuel);

                if (stat==11)
                {
                    print_auto(car);
                    printf("Maufactured date:");
                    print_date(car.manufact);
                    printf("\nPurchased date:");
                    print_date(car.purchase);
                    printf("\nTank capacity and current fuel");
                    print_tank(car.tank);
                }
           }    

        getch();  // Just used to keep the data on the sreen for testing pupose
        return(0);
}


    int scan_date(date_t *date)
    {
        int res;
        res=scanf("%d %d %d", &(*date).month, &(*date).day, &(*date).year);
        if(res==3)
         res=1;
        else if(res !=EOF)
         res=0;
        return(res);
    }

    int scan_tank(tank_t *tank)
    {
        int res;
        res=scanf("%lf %lf", &(*tank).capacity, &(*tank).curent_Fuel);
        if(res==2)
         res=1;
        else if(res !=EOF)
         res=0;
        return(res);
    }

    int scan_automobile(auto_t *automo)
    {
        int res;
        res=scanf("%s %s %d %d %d %d %d %d %d %lf %lf", &(*automo).make,
                                                        &(*automo).model,
                                                        &(*automo).odometer,
                                                        &(*automo).manufact.month,
                                                        &(*automo).manufact.day,
                                                        &(*automo).manufact.year,
                                                        &(*automo).purchase.month,
                                                        &(*automo).purchase.day,
                                                        &(*automo).purchase.year,
                                                        &(*automo).tank.capacity,
                                                        &(*automo).tank.curent_Fuel);
        if(res==11)
         res=1;
        else if(res !=EOF)
         res=0;
        return(res);
    }

    void print_date(date_t da)
    {
        printf("\n%d-%d-%d", da.month, da.day, da.year);
    }

    void print_tank(tank_t ta)
    {
        printf("\n%2.2lf %2.2lf\n", ta.capacity, ta.curent_Fuel);
    }

    void print_auto(auto_t au)
    {
        printf("\nVehicle \n%s %s %d %d %d %d %d %d %d %lf %lf\n", au.make,
                                                     au.model,
                                                     au.odometer,
                                                     au.manufact.month,
                                                     au.manufact.day,
                                                     au.manufact.year,
                                                     au.purchase.month,
                                                     au.purchase.day,
                                                     au.purchase.year,
                                                     au.tank.capacity,
                                                     au.tank.curent_Fuel);
    }

2 个答案:

答案 0 :(得分:0)

我的代码中有一些我不理解的东西可能会让你遇到麻烦:

    if (Car_data==NULL){
       printf("ERROR: File failed to open");
       getch();
       exit(0);
       fclose(Car_data);}

为什么在处理错误时使用exit(0)?可以查看this

if(stat==11)
print_auto(car);
// rest of the code

除了缩进,我不确定你在这里, if(stat == 11),只会执行 print_auto(car)。 如果你想让条件和循环包含更多的代码,你需要这样做:

if(condition)
{
   // code
}
else
{
  // code
}
loop()
{
  // code
}

我认为你的错误很可能来自于此。

答案 1 :(得分:0)

这不是一个真正的答案,但您格式化代码的方式非常容易出错:

例如这段代码:

res=scanf("%lf %lf", &(*tank).capacity, &(*tank).curent_Fuel);
if(res==2)
res=1;
else if(res !=EOF)
res=0;
return(res);

应该像这样格式化:

res=scanf("%lf %lf", &(*tank).capacity, &(*tank).curent_Fuel);
if(res==2)
  res=1;
else if(res !=EOF)
  res=0;
return(res);