简体   繁体   中英

Can't show the data from the file properly in C program

I faced a little problem right now which is the C program can't print the result on the screen properly. Even the program can write input from the user and print it into Notepad. The type of file used is random access file and I need to write data into a binary file named toyota.txt Anyone can teach me what is the problem there? Thank you so much!

/*program 1: write the data into file toyota.txt*/

#include<stdio.h>

struct vehicledata{
    char vnum[100];
    int cnum;
    float f50[100];
    float f100[100];
    float f200[100];
    float f400[100];
    float f800[100];
};

int main()
{
    FILE *fptr;
    struct vehicledata toyota;
    if((fptr = fopen("toyota.txt","wb")) == NULL)
    {
        printf("Error! File fail opened!");
    }
    else
    {
        printf("This program can create a text file named toyota.txt \n");
        printf("and save the value of car noise level(dB) with\n different frequency and vehicle numbers into file\n\n");
        printf("Enter car number(1 to 10, enter 0 to end input)\n");
        printf("? ");
        scanf("%d", &toyota.cnum);

        while (toyota.cnum != 0)
        {
            printf("\nEnter Vehicle and car noise level(dB) at 50Hz, 100Hz, 200Hz, 400Hz and 800Hz\n");
            printf("? ");
            scanf("%s %f %f %f %f %f", toyota.vnum, &toyota.f50, &toyota.f100, &toyota.f200, &toyota.f400, &toyota.f800);
            fseek(fptr, (toyota.cnum - 1) * sizeof(struct vehicledata), SEEK_SET);
            fwrite(&toyota, sizeof(struct vehicledata), 1, fptr);
            printf("\nEnter car number (1 to 100, 0 to end input) \n");
            scanf("%d", &toyota.cnum);
        }
    }

    fclose(fptr);
    return 0;
}
/*program 2 : reads the data from the file toyota.txt and find
the car that emanates the minimum noise at 200Hz*/
#include <stdio.h>
#include <stdlib.h>

struct vehicledata{
    char vnum[100];
    int cnum;
    float f50[100];
    float f100[100];
    float f200[100];
    float f400[100];
    float f800[100];
};

int main()
{
    FILE *fptr;
    struct vehicledata toyota;
    if ((fptr = fopen("toyota.txt","rb")) == NULL)//open and read data in the file
    {
        printf("Error! File fail opened!");
    }
    else
    {
        printf("This program can read and display the data from the toyota.txt\n and find car that have minimum noise in 200Hz\n\n");
        printf("\nThe information in the toyota.txt are shown at below:\n");
        printf("\n\t\t\t\tCar Noise Level(dB)\t\t\t");
        printf("\n=======================================================================\n");
        printf("%s\t\t%s\t%s\t%s\t%s\t%s\t\n","Vehicle Number","50Hz","100Hz","200Hz","400Hz","800Hz");
        printf("=======================================================================\n");
        while(!feof(fptr))
        {
            fread(&toyota, sizeof(struct vehicledata), 1, fptr);
            if(toyota.cnum != 0)
            {
                printf("%s\t\t\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f", toyota.vnum, toyota.f50, toyota.f100, toyota.f200, toyota.f400, toyota.f800);
            }
        }
    }
    fclose(fptr);
    return 0;
}

输出

需要输入的数据

Update: Thank you Mr. Bo R answer my question. After I correct my code, now I faced another problem which is the program will print twice for the last line of data(figure below). For this situation, can I know what is the problem with my code? 新问题

The major problem with the implementation is that the vehicledata structure is wrong.

Try

struct vehicledata {
    char vnum[100];
    int cnum;
    float f50;
    float f100;
    float f200;
    float f400;
    float f800;
};

and it will work a lot easier.

You don't want to collect 500 (5 * 100) different freqeuncey data for each vehicle it seems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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