简体   繁体   English

C程序:从文件读取和复制到结构?

[英]C Program: Reading from file and copying to a struct?

I'm working on an assignment for a class and I'm having trouble applying file input/output and structs together. 我正在为一个班级工作,我在应用文件输入/输出和结构时遇到了麻烦。 I have this code that reads different cars from a file and copies their information into an array of structs. 我有这个代码从文件中读取不同的汽车并将其信息复制到一个结构数组。

#include <stdio.h>

#define MAX_LEN 1000 
#define NAME_LEN 30

struct car{
    char make[NAME_LEN + 1];
    char model[NAME_LEN + 1];
    int year;
    int cmpg;
    int hmpg;
    int avgmpg;
};
.
.
.
int main()
{
    int i;

    struct car cars[MAX_LEN];

    FILE* pFile;
    pFile = fopen("cars.txt", "r");

    .
    .
    .
    .

    for(i = 0; i < MAX_LEN; i++)
    {
        while(!feof(pFile) && !ferror(pFile))
            {
            fscanf(pFile, "%s%s%d%d%d ", cars[i].make, cars[i].model, &cars[i].year, &cars[i].cmpg, &cars[i].hmpg);
            cars[i].avgmpg = (cars[i].cmpg + cars[i].hmpg) / 2;
        }
    }

    selection_sort(cars, MAX_LEN);

    FILE* outFile;
    outFile = fopen("sorted_cars.txt", "w");

    fprintf(outFile, "Make     Model year  city mpg  highway mpg average mpg\n");

    for(i = 0; i < MAX_LEN; i++);
    {   
        fprintf(outFile, "%s %s %d %-2d %-10d %-12d\n", cars[i].make, cars[i].model, cars[i].year, cars[i].cmpg, cars[i].hmpg, cars[i].avgmpg); 
        printf("%s %s %d %d %d %d\n", cars[i].make, cars[i].model, cars[i].year, cars[i].cmpg, cars[i].hmpg, cars[i].avgmpg);
    }


    fclose(pFile);
    fclose(outFile);
    return 0;
    }

This is what the .txt file looks like: 这就是.txt文件的样子:

Mercury Sable 2009 18 28
Jeep Wrangler 2016 17 21
Honda Civic 2015 31 41
Toyota Corolla 2015 30 42
Toyota Prius 2010 51 48
Ford Escape 2013 23 33
Ford Fusion 2013 25 37
Acura MDX 2014 20 28
Lexus RX 2013 32 28

I inserted a print function into my code so I could see if the cars read properly, but when I run the program, this is the output: 我在我的代码中插入了一个打印功能,所以我可以看到汽车是否正常读取,但是当我运行程序时,这是输出:

▒▒▒8 0 0 1465899048 32767 ▒▒▒800 1465899048 32767

I have absolutely no idea why this is happening. 我完全不知道为什么会这样。 Can anybody help? 有人可以帮忙吗?

First remove while() and put if(!feof(pFile) && !ferror(pFile)) . 首先删除while()并输入if(!feof(pFile) && !ferror(pFile)) In your fscanf() for cars file put spaces between type specifiers. 在你的fscanf() for cars文件中,在类型说明符之间放置空格。 It should be like this 它应该是这样的

fscanf(pFile, "%s %s %d %d %d", cars[i].make, cars[i].model, &cars[i].year, &cars[i].cmpg, &cars[i].hmpg); cars[i].avgmpg = (cars[i].cmpg + cars[i].hmpg) / 2;

And before directly writing to the file, first print the values of struct in console only, and whether you are getting it in proper way or not. 在直接写入文件之前,首先只在控制台中打印struct的值,以及是否以正确的方式获取它。

And the last modification is Instead of using this 最后一个修改是使用它而不是使用它

fprintf(outFile, "%s %s %d %-2d %-10d %-12d\n", cars[i].make, cars[i].model, cars[i].year, cars[i].cmpg, cars[i].hmpg, cars[i].avgmpg);

Use this 用这个

fprintf(outFile, "%s %s %d %d %d %d\n", cars[i].make, cars[i].model, cars[i].year, cars[i].cmpg, cars[i].hmpg, cars[i].avgmpg);

I hope this will help you. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM