C中的文件处理计算
File Handling Calculation In C
我想计算体重指数并将其写入 file.I如您所见,我尝试在另一个函数中计算它,但输出只是 0.Then 我尝试了不同的方法像 int 计算 returns 到 w/h*h 的函数,但是其中 none 给了我正确的 answers.I 找不到其他方法。
#include <stdio.h>
struct person
{
int personId;
double height;
double weight;
double BMI;
};
void calculate (double h, double w)
{
struct person p1;
p1.BMI = w / h*h ;
}
void write () {
FILE *file;
struct person p1;
file = fopen("bmi.txt","w");
if (file == NULL)
{
printf("Error");
}
else
{
printf("Person ID: "); scanf("%d",&p1.personId);
printf("Height: "); scanf("%lf",&p1.height);
printf("Weight: "); scanf("%lf",&p1.weight);
calculate(p1.height,p1.weight);
fwrite(&p1,sizeof(p1),1,file);
}
fclose(file);
}
void read() {
FILE *file;
struct person p1;
file = fopen("bmi.txt","r");
if (file == NULL)
{
printf("Error");
}
else
{
while (!feof(file))
{
fread(&p1,sizeof(p1),1,file);
printf("Person ID: %d\n",p1.personId);
printf("Height: %f\n",p1.height);
printf("Weight: %f\n",p1.weight);
printf ("BMI: %f\n",p1.BMI);
}
}
fclose(file);
}
int main () {
write();
read();
}
虽然掌握了作业的一些概念,但错误较多:
在main
中,你通常会先read
数据,然后再write
它们。
一个没有return任何东西的函数是没用的:检查calculate
.
在 write
中,您从 stdin
获取信息。您应该使用从输入文件中读取的信息。检查 write
的函数声明:它不应该将 person
作为其输入参数吗?
不应该read
return吗?例如刚刚阅读的信息?使用 malloc
获取可以 return.
的内存
另请参阅其他人的评论以获取更多建议。
#include <stdio.h>
struct person {
int personId;
double height;
double weight;
double BMI;
};
double calculate (double h, double w){//change to return result
return w / (h*h);//Need parentheses
}
void write_person(void){
FILE *file;
struct person p1;
if ((file = fopen("bmi.dat", "ab")) == NULL){
perror("fopen in write_person");
} else {
printf("Person ID : "); scanf("%d", &p1.personId);
printf("Height(m) : "); scanf("%lf",&p1.height);
printf("Weight(kg): "); scanf("%lf",&p1.weight);
p1.BMI = calculate(p1.height,p1.weight);
fwrite(&p1, sizeof(p1), 1, file);
fclose(file);//Should be into else-block
}
}
void read_person(void){
FILE *file;
struct person p1;
if ((fopen("bmi.dat","rb")) == NULL){
perror("fopen in read_person");
} else {
while (fread(&p1, sizeof(p1), 1, file) > 0){//don't use !feof(file) because EOF occurs at fread So Display of incorrect data.
printf("Person ID: %d\n", p1.personId);
printf("Height : %.3f(m)\n", p1.height);
printf("Weight : %.1f(kg)\n", p1.weight);
printf("BMI : %.2f\n\n", p1.BMI);
}
fclose(file);
}
}
int main (void) {
write_person();
read_person();
return 0;
}
我想计算体重指数并将其写入 file.I如您所见,我尝试在另一个函数中计算它,但输出只是 0.Then 我尝试了不同的方法像 int 计算 returns 到 w/h*h 的函数,但是其中 none 给了我正确的 answers.I 找不到其他方法。
#include <stdio.h>
struct person
{
int personId;
double height;
double weight;
double BMI;
};
void calculate (double h, double w)
{
struct person p1;
p1.BMI = w / h*h ;
}
void write () {
FILE *file;
struct person p1;
file = fopen("bmi.txt","w");
if (file == NULL)
{
printf("Error");
}
else
{
printf("Person ID: "); scanf("%d",&p1.personId);
printf("Height: "); scanf("%lf",&p1.height);
printf("Weight: "); scanf("%lf",&p1.weight);
calculate(p1.height,p1.weight);
fwrite(&p1,sizeof(p1),1,file);
}
fclose(file);
}
void read() {
FILE *file;
struct person p1;
file = fopen("bmi.txt","r");
if (file == NULL)
{
printf("Error");
}
else
{
while (!feof(file))
{
fread(&p1,sizeof(p1),1,file);
printf("Person ID: %d\n",p1.personId);
printf("Height: %f\n",p1.height);
printf("Weight: %f\n",p1.weight);
printf ("BMI: %f\n",p1.BMI);
}
}
fclose(file);
}
int main () {
write();
read();
}
虽然掌握了作业的一些概念,但错误较多:
在
main
中,你通常会先read
数据,然后再write
它们。一个没有return任何东西的函数是没用的:检查
calculate
.在
write
中,您从stdin
获取信息。您应该使用从输入文件中读取的信息。检查write
的函数声明:它不应该将person
作为其输入参数吗?不应该
read
return吗?例如刚刚阅读的信息?使用malloc
获取可以 return. 的内存
另请参阅其他人的评论以获取更多建议。
#include <stdio.h>
struct person {
int personId;
double height;
double weight;
double BMI;
};
double calculate (double h, double w){//change to return result
return w / (h*h);//Need parentheses
}
void write_person(void){
FILE *file;
struct person p1;
if ((file = fopen("bmi.dat", "ab")) == NULL){
perror("fopen in write_person");
} else {
printf("Person ID : "); scanf("%d", &p1.personId);
printf("Height(m) : "); scanf("%lf",&p1.height);
printf("Weight(kg): "); scanf("%lf",&p1.weight);
p1.BMI = calculate(p1.height,p1.weight);
fwrite(&p1, sizeof(p1), 1, file);
fclose(file);//Should be into else-block
}
}
void read_person(void){
FILE *file;
struct person p1;
if ((fopen("bmi.dat","rb")) == NULL){
perror("fopen in read_person");
} else {
while (fread(&p1, sizeof(p1), 1, file) > 0){//don't use !feof(file) because EOF occurs at fread So Display of incorrect data.
printf("Person ID: %d\n", p1.personId);
printf("Height : %.3f(m)\n", p1.height);
printf("Weight : %.1f(kg)\n", p1.weight);
printf("BMI : %.2f\n\n", p1.BMI);
}
fclose(file);
}
}
int main (void) {
write_person();
read_person();
return 0;
}