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

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

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

问题

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

  1. #include <stdio.h>
  2. int main(){
  3. struct Student
  4. {
  5. char* matricule;
  6. char* nom;
  7. char* prenom;
  8. };
  9. Student student[5]; // 这里应该是 student[5],而不是 student[4]
  10. student[0].matricule = "m001";
  11. student[0].nom = "Boussaha";
  12. student[0].prenom = "Borhanedine";
  13. student[1].matricule = "m002";
  14. student[1].nom = "Toaba";
  15. student[1].prenom = "Anes";
  16. student[2].matricule = "m003";
  17. student[2].nom = "Laamari";
  18. student[2].prenom = "Loqmane";
  19. student[3].matricule = "m004";
  20. student[3].nom = "Dellachi";
  21. student[3].prenom = "Amir";
  22. student[4].matricule = "m005";
  23. student[4].nom = "Zenfour";
  24. student[4].prenom = "Abdelmouiz";
  25. FILE *f;
  26. f = fopen("list.data", "wb");
  27. for (int i = 0; i < 5; i++)
  28. {
  29. fwrite(student[i].matricule, sizeof(char), strlen(student[i].matricule), f);
  30. fwrite(student[i].nom, sizeof(char), strlen(student[i].nom), f);
  31. fwrite(student[i].prenom, sizeof(char), strlen(student[i].prenom), f);
  32. }
  33. fclose(f); // 没有关闭文件流
  34. return 0; // 函数需要返回一个值
  35. }

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

英文:

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 :

  1. #include&lt;stdio.h&gt;
  2. int main(){
  3. struct Student
  4. {
  5. char* matricule;
  6. char* nom ;
  7. char* prenom;
  8. };
  9. Student student[4];
  10. student[0].matricule = &quot;m001&quot;;
  11. student[0].nom = &quot;Boussaha&quot;;
  12. student[0].prenom = &quot;Borhanedine&quot;;
  13. student[1].matricule = &quot;m002&quot;;
  14. student[1].nom = &quot;Toaba&quot;;
  15. student[1].prenom = &quot;Anes&quot;;
  16. student[2].matricule = &quot;m003&quot;;
  17. student[2].nom = &quot;Laamari&quot;;
  18. student[2].prenom = &quot;Loqmane&quot;;
  19. student[3].matricule = &quot;m004&quot;;
  20. student[3].nom = &quot;Dellachi&quot;;
  21. student[3].prenom = &quot;Amir&quot;;
  22. student[4].matricule = &quot;m005&quot;;
  23. student[4].nom = &quot;Zenfour&quot;;
  24. student[4].prenom = &quot;Abdelmouiz&quot;;
  25. FILE *f;
  26. f = fopen(&quot;list.data&quot; , &quot;wb&quot;);
  27. for (int i = 0; i &lt; 5; i++)
  28. {
  29. fwrite(student[i].matricule , sizeof(student[i].matricule) , 5 , f);
  30. fwrite(student[i].nom , sizeof(student[i].nom) , 5 , f);
  31. fwrite(student[i].prenom , sizeof(student[i].prenom) , 5 , f);
  32. }
  33. }

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:

确定