英文:
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<stdio.h>
int main(){
struct Student
{
char* matricule;
char* nom ;
char* prenom;
};
Student 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(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 meansstudent[0]
throughstudent[3]
is valid, butstudent[4]
is not valid. Accessingstudent[4]
effectively makes you program behave unpredictably. Set it toStudent student[5]
. -
f = fopen("list.data" , "wb");
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 thatsizeof
does not do what you think it does. And I bet you don't seem to know why that5
is there either. Replacesizeof
withstrlen
, and replace5
with1
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论