如何在C中使用结构体和数组来利用fwrite函数填充文件?

huangapple go评论57阅读模式
英文:

How do I use struct and arrays to fill a file using fwrite function in C?

问题

我想创建一个程序,使用**fwrite()**将5个学生的属性(学号,名,姓)填充到文件中,我尝试使用结构体和数组来解决这个问题,如下所示:

#include <stdio.h>
int main(){

    struct Student
    {
        char* matricule;
        char* nom;
        char* prenom;
    };

    Student student[5]; // 这里应该是 student[5],而不是 student[4]

    student[0].matricule = "m001";
    student[0].nom = "Boussaha";
    student[0].prenom = "Borhanedine";

    student[1].matricule = "m002";
    student[1].nom = "Toaba";
    student[1].prenom = "Anes";

    student[2].matricule = "m003";
    student[2].nom = "Laamari";
    student[2].prenom = "Loqmane";

    student[3].matricule = "m004";
    student[3].nom = "Dellachi";
    student[3].prenom = "Amir";

    student[4].matricule = "m005";
    student[4].nom = "Zenfour";
    student[4].prenom = "Abdelmouiz";

    FILE *f;
    f = fopen("list.data", "wb");

    for (int i = 0; i < 5; i++)
    {
        fwrite(student[i].matricule, sizeof(char), strlen(student[i].matricule), f);
        fwrite(student[i].nom, sizeof(char), strlen(student[i].nom), f);
        fwrite(student[i].prenom, sizeof(char), strlen(student[i].prenom), f);
    }

    fclose(f); // 没有关闭文件流

    return 0; // 函数需要返回一个值
}

我猜想并希望代码没有错误。当我调试程序时,文件被创建,但是我发现它是空的。我在想为什么文件是空的,而我期望文件中应该填充有每个学生的属性?

英文:

I want to create a program that fills a file with properties of 5 students ( matricule , firstname , lastname ) using fwrite(), i tried to solve that using struct with arrays as shown bellow :

#include&lt;stdio.h&gt;
int main(){

    struct Student
    {
        char* matricule;
        char* nom ;
        char* prenom;
    };

    Student student[4];

    student[0].matricule = &quot;m001&quot;;
    student[0].nom = &quot;Boussaha&quot;;
    student[0].prenom = &quot;Borhanedine&quot;;

    student[1].matricule = &quot;m002&quot;;
    student[1].nom = &quot;Toaba&quot;;
    student[1].prenom = &quot;Anes&quot;;

    student[2].matricule = &quot;m003&quot;;
    student[2].nom = &quot;Laamari&quot;;
    student[2].prenom = &quot;Loqmane&quot;;

    student[3].matricule = &quot;m004&quot;;
    student[3].nom = &quot;Dellachi&quot;;
    student[3].prenom = &quot;Amir&quot;;

    student[4].matricule = &quot;m005&quot;;
    student[4].nom = &quot;Zenfour&quot;;
    student[4].prenom = &quot;Abdelmouiz&quot;;

    
    FILE *f;
    f = fopen(&quot;list.data&quot; , &quot;wb&quot;);

    for (int i = 0; i &lt; 5; i++)
    {
        fwrite(student[i].matricule , sizeof(student[i].matricule) , 5 , f);
        fwrite(student[i].nom , sizeof(student[i].nom) , 5 , f);
        fwrite(student[i].prenom , sizeof(student[i].prenom) , 5 , f);
    }
}

I guess and hope nothing is wrong in the code.

When I debug the program, the file gets created but I find it empty. I am wondering why it is empty when I was expecting the file to be filled with the properties of each student?

答案1

得分: 0

  • Student student[4]; 分配了足够的空间用于 4 个元素。这意味着 student[0]student[3] 是有效的,但 student[4] 是无效的。访问 student[4] 会导致程序行为不可预测。将其更改为 Student student[5]

  • f = fopen("list.data", "wb"); 是可以的,但你有检查错误吗? if (f == 0) 表示发生了错误。小心总比抱歉好。

  • fwrite(student[i].XYZ, sizeof(student[i].XYZ), 5, f); 是非常错误的。我不确定你是否是新手,但我可以告诉你 sizeof 不是你想象的那样工作的。而且我敢打赌你似乎不知道为什么有个 5 在那里。请将 sizeof 替换为 strlen,并将 5 替换为 1

英文:
  • Student student[4]; allocates enough room for 4 elements. This means student[0] through student[3] is valid, but student[4] is not valid. Accessing student[4] effectively makes you program behave unpredictably. Set it to Student student[5].

  • f = fopen(&quot;list.data&quot; , &quot;wb&quot;); is okay, but did you check for errors? if(f==0) then an error has occurred. Better safe than sorry.

  • fwrite(student[i].XYZ , sizeof(student[i].XYZ) , 5 , f); is very wrong. I'm not sure if you're new to C but I can tell you that sizeof does not do what you think it does. And I bet you don't seem to know why that 5 is there either. Replace sizeof with strlen, and replace 5 with 1.

huangapple
  • 本文由 发表于 2023年2月16日 17:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定