如何使用fwrite()函数在文件中一次性写入结构成员? #包括 #包括 #包括 #包括 结构学生{ 字符*名称; char*addr; 智力年龄; int-clas; }*斯图; int main() { 文件*fp; int选择,另一个; 大小(不包括大小);; 尺寸与长度; struct student*stu=(struct student*)malloc(sizeof(struct student)); stu->name=(char*)malloc(sizeof(char)*20); stu->addr=(char*)malloc(sizeof(char)*20); recsize=sizeof(*stu); fp=fopen(“student.txt”,“a+”); 如果(fp==NULL) { fp=fopen(“student.txt”,“w+”); 如果(fp==NULL) { printf(“无法打开文件”); 出口(1); } } 做 { fseek(fp,1,SEEK_END); printf(“请输入学生详细信息”); printf(“学生姓名:”); scanf(“%s”,stu->name); printf(“地址:”); scanf(“%s”,stu->addr); printf(“类:”); scanf(“%s”和&stu->clas); printf(“年龄:”); scanf(“%s”,&stu->age); fwrite(stu,recsize,1,fp); printf(“添加另一个输入1?\n”); scanf(“%d”和另一个); }而(另一个=1); fclose(fp); 免费(stu); }
我有C语言的代码,它有一个结构Student。我正在尝试从用户获取所有结构成员的值。内存分配给结构和两个成员*name和*addr。当我尝试在Student.txt文件中使用fwrite()函数写入这些值时,它会显示随机输出(ཀའ㌱䔀8.䵁ཀའ㈱䔀1.䵁)类似于文件中的内容,但它不是可读形式。请提供使用fwrite()函数在文件中写入结构成员的最佳方法。对于如何使用fwrite()函数在文件中一次性写入结构成员? #包括 #包括 #包括 #包括 结构学生{ 字符*名称; char*addr; 智力年龄; int-clas; }*斯图; int main() { 文件*fp; int选择,另一个; 大小(不包括大小);; 尺寸与长度; struct student*stu=(struct student*)malloc(sizeof(struct student)); stu->name=(char*)malloc(sizeof(char)*20); stu->addr=(char*)malloc(sizeof(char)*20); recsize=sizeof(*stu); fp=fopen(“student.txt”,“a+”); 如果(fp==NULL) { fp=fopen(“student.txt”,“w+”); 如果(fp==NULL) { printf(“无法打开文件”); 出口(1); } } 做 { fseek(fp,1,SEEK_END); printf(“请输入学生详细信息”); printf(“学生姓名:”); scanf(“%s”,stu->name); printf(“地址:”); scanf(“%s”,stu->addr); printf(“类:”); scanf(“%s”和&stu->clas); printf(“年龄:”); scanf(“%s”,&stu->age); fwrite(stu,recsize,1,fp); printf(“添加另一个输入1?\n”); scanf(“%d”和另一个); }而(另一个=1); fclose(fp); 免费(stu); },c,data-structures,fwrite,file-handling,C,Data Structures,Fwrite,File Handling,我有C语言的代码,它有一个结构Student。我正在尝试从用户获取所有结构成员的值。内存分配给结构和两个成员*name和*addr。当我尝试在Student.txt文件中使用fwrite()函数写入这些值时,它会显示随机输出(ཀའ㌱䔀8.䵁ཀའ㈱䔀1.䵁)类似于文件中的内容,但它不是可读形式。请提供使用fwrite()函数在文件中写入结构成员的最佳方法。对于ints,您需要使用%d而不是%s #include<stdio.h> #include<conio.h> #inc
int
s,您需要使用%d
而不是%s
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student{
char *name;
char *addr;
int age;
int clas;
}*stu;
int main()
{
FILE *fp;
int choice,another;
size_t recsize;
size_t length;
struct student *stu=(struct student *)malloc(sizeof(struct student));
stu->name=(char *) malloc(sizeof(char)*20);
stu->addr=(char*)malloc(sizeof(char)*20);
recsize=sizeof(*stu);
fp=fopen("student.txt","a+");
if(fp==NULL)
{
fp=fopen("student.txt","w+");
if(fp==NULL)
{
printf("cannot open the file");
exit(1);
}
}
do
{
fseek(fp,1,SEEK_END);
printf("Please Enter student Details\n");
printf("Student Name: ");
scanf("%s",stu->name);
printf("Address: ");
scanf("%s",stu->addr);
printf("Class: ");
scanf("%s",&stu->clas);
printf("Age: ");
scanf("%s",&stu->age);
fwrite(stu,recsize,1,fp);
printf("Add another Enter 1 ?\n");
scanf("%d",&another);
}while(another==1);
fclose(fp);
free(stu);
}
应该是
printf("Class: ");
scanf("%s",&stu->clas);
printf("Age: ");
scanf("%s",&stu->age);
正如@davidhoelzer在评论中指出的那样:你写的是指针的值,而不是它们包含的内容,改变
printf("Class: ");
scanf("%d",&stu->clas);
printf("Age: ");
scanf("%d",&stu->age);
到
并删除这些行:
struct student{
char name[20];
char addr[20];
因为你写的是指针的值,而不是它们包含的内容。除非将该结构更改为静态大小的分配(数组),否则您将无法以这种方式写入该结构代码>
scanf(“%s”,&stu->age)代码>您正在将字符串写入int容器,这可能会损坏memory@DavidHoelzer,是否可以写入指针所包含的内容?我知道,如果将*name更改为name[20]并且将*addr更改为addr[20],我的代码将正常工作。在C中,当调用任何内存分配函数:(malloc、calloc、realloc)时,返回值的类型为void*
,因此可以分配给任何指针。强制转换返回值只会使代码变得混乱,使其更难理解、调试和维护。建议删除返回值的强制转换。注意:表达式:sizeof(char)
在标准中定义为1,将任何值乘以1都没有效果,只是将代码弄乱了。建议删除表达式。在调用fopen()
时,如果文件不存在,打开进行追加将创建文件,因此如果失败,打开进行写入+读取将无法解决问题。我更正了它,但输出是随机字符。因为我使用的是指针,所以使用fwrite在文件中写入结构成员的最佳方式是什么。如果您想要格式化文本,请使用fwrite写入原始字节fprintf@EttoreBarattelli谢谢你提供的信息。那么,我们如何以可读的格式读取这些原始字节,有什么功能吗?不确定“读取这些原始字节”是什么意思,如果您想将可读数据输出到文件中,可以执行fprintf(fp,“%s%s%d%d\n”,stu->name,std->addr,stu->age,stu->clas)
@EttoreBarattelli您说过fwrite写原始字节,所以我想说的是,有没有函数可以读取fwrite()编写的c中的原始字节。
struct student{
char name[20];
char addr[20];
stu->name=(char *) malloc(sizeof(char)*20);
stu->addr=(char*) malloc(sizeof(char)*20);