无法在 C 程序中正确显示文件中的数据
Can't show the data from the file properly in C program
我现在遇到了一个小问题,即 C 程序无法在屏幕上正确打印结果。甚至程序也可以写入用户的输入并将其打印到记事本中。
使用的文件类型是随机访问文件,我需要将数据写入名为 toyota.txt 的二进制文件
任何人都可以教我那里有什么问题?非常感谢!
/*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;
}
实现的主要问题是vehicledata
结构错误。
尝试
struct vehicledata {
char vnum[100];
int cnum;
float f50;
float f100;
float f200;
float f400;
float f800;
};
工作起来会容易很多。
您似乎不想为每辆车收集 500 (5 * 100) 个不同的频率数据。
我现在遇到了一个小问题,即 C 程序无法在屏幕上正确打印结果。甚至程序也可以写入用户的输入并将其打印到记事本中。 使用的文件类型是随机访问文件,我需要将数据写入名为 toyota.txt 的二进制文件 任何人都可以教我那里有什么问题?非常感谢!
/*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;
}
实现的主要问题是vehicledata
结构错误。
尝试
struct vehicledata {
char vnum[100];
int cnum;
float f50;
float f100;
float f200;
float f400;
float f800;
};
工作起来会容易很多。
您似乎不想为每辆车收集 500 (5 * 100) 个不同的频率数据。