【问题标题】:File Handling Calculation In C [closed]C中的文件处理计算[关闭]
【发布时间】:2016-05-08 17:23:42
【问题描述】:

我想计算体重指数并将其写入文件。如您所见,我尝试在另一个函数中计算它,但输出仅为 0。然后我尝试了不同的函数,如 int calculate 返回w/h*h 但他们都没有给我正确的答案。我找不到其他方法。

#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();
}

【问题讨论】:

  • h*h --> (h*h), void calculate (double h, double w) --> void calculate (struct person *p)
  • while ( !feof)) {...} 再次。顺便说一句,read()write() 是函数的可怕名称。只是说...
  • 您使用fread 读取输入数据,它读取二进制数据。您是否从文件中获得了预期的数据? (您似乎更想读取文本数据。)
  • 开始您的代码编译或提供确实编译的实际代码。为此启用警告并在询问之前清理它们。
  • 感谢您的评论。在询问之前我已经阅读了所有内容,这对我来说似乎是正确的。但是我会再次阅读,下次我会更加小心。

标签: c file file-handling


【解决方案1】:
#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;
}

【讨论】:

    【解决方案2】:

    虽然你掌握了作业的一些概念,但还是有很多错误:

    1. main 中,您通常首先要read 数据,然后再write 他们。

    2. 不返回任何东西的函数是没用的:检查calculate

    3. write 中,您从stdin 获取信息。您应该使用从输入文件中读取的信息。检查write 的函数声明:它不应该将person 作为其输入参数吗?

    4. read 不应该返回一些东西吗?例如刚刚阅读的信息?使用malloc 获取可以返回的内存。

    请参阅其他人的 cmets 以获得更多建议。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 2013-10-31
      相关资源
      最近更新 更多