C - f扫描读取错误



提醒一下,严格来说这不是家庭作业。它不值得一个分数,它只是为了练习。所以不要认为我是为了得到更好的成绩而作弊。

这是我正在工作的函数:

Car* readCars(char* filename) {
    FILE* fp = fopen( filename, "r" );
    if( fp == NULL ) {
        printf( "Error: Invalid File!" );
        exit(1);
    }
    Car cars[5];
    Car *carPtr;
    int i = 0;
    while( 1 ) {
        fscanf( fp, "%s %s %s %i", cars[i].color, cars[i].model, cars[i].brand, &cars[i].year );
        if( feof( fp ) ) {
            break;
        }
        i++;
    }
    carPtr = cars;
    fclose( fp );
    return carPtr;
}

汽车结构:

typedef struct _car {
    char* color;
    char* model;
    char* brand;
    int year;
} Car;

At "return cars;"我得到"不兼容的类型时返回类型'struct Car *'期待'Car'"

这里是我从main调用函数的地方,以及我在脑海中为Car"对象"所做的声明。

Car *car;
car = readCars( carFileName );

1)。当从文件中扫描时,我得到一个错误。

结构Car中没有空格

typedef struct _car {
    // char* color;
    char color[20];
    int year;
} Car;
if (2 != fscanf(fp, "%19s%d", cars[i].color, &cars[i].year) {
  break;
}

最新更新