C语言把结构体写入文件|C语言怎么把结构体写入文件

|

① c语言将结构体写入文件

整个结构体写入 读出来时整个结构体读出 类型没什么关系 二进制读写还管你变量什么类型

② C语言中,将多个结构体数据写到一个文件中,应该如何读取

C语言把一个结构体数组写入文件分三步:1、以二进制写方式(wb)打开文件2、调用写入函数fwrite()将结构体数据写入文件3、关闭文件指针相应的,读文件也要与之匹配:1、以二进制读方式(rb)打开文件2、调用读文件函数fread()读取文件中的数据到结构体变量3、关闭文件指针参考代码如下:

#include<stdio.h>structstu{charname[30];intage;doublescore;};intread_file();intwrite_file();intmain(){if(write_file()<0)//将结构体数据写入文件return-1;read_file();//读文件,并显示数据return0;}intwrite_file(){FILE*fp=NULL;structstustudent={"zhangsan",18,99.5};fp=fopen("stu.dat","wb");//b表示以二进制方式打开文件if(fp==NULL)//打开文件失败,返回错误信息{printf("openfileforwriteerror");return-1;}fwrite(&student,sizeof(structstu),1,fp);//向文件中写入数据fclose(fp);//关闭文件return0;}intread_file(){FILE*fp=NULL;structstustudent;fp=fopen("stu.dat","rb");//b表示以二进制方式打开文件if(fp==NULL)//打开文件失败,返回错误信息{printf("openfileforreaderror");return-1;}fread(&student,sizeof(structstu),1,fp);//读文件中数据到结构体printf("name="%s"age=%dscore=%.2lf",student.name,student.age,student.score);//显示结构体中的数据fclose(fp);//关闭文件return0;}

③ C语言结构体如何存入文件

最好用2进制方法打开文件,用fwrite 写文件。读时也用用2进制方法打开文件,用fread读.这样,写的时候按整个结构写,读也按整个结构读,字符串有空白也没关系。FILE *fp; fp=fopen("my.dat","wb");fwrite(&stu,sizeof(stu),1,fp); // 写1个结构for (i=0;i<10;i++) fwrite(&student[i],sizeof(student),1,fp); // 写10个结构fclose(fp); fp=fopen("my.dat","rb");fread(&stu,sizeof(stu),1,fp); // 读1个结构for (i=0;i<10;i++) fread(&student[i],sizeof(student),1,fp); // 读 10个结构fclose(fp);

④ C语言结构体数组写入文件的问题

读的时候路径不对。。还有就是写入的文件是以字符的形式写入的。所以int型要转一下。。具体如下#include"stdio.h"#include"conio.h"#include"stdlib.h"structnode{inta;charb[2];};main(){FILE*p;charch;inti=0,len=sizeof(structnode);structnodestu[2];chartemp[2];for(i=0;i<2;i++){printf("information:\n");gets(temp);stu[i].a=atoi(temp);gets(stu[i].b);}//输入结构体数组for(i=0;i<2;i++){printf("%d%s\n",stu[i].a,stu[i].b);}//检查是否输入成功if(stu[0].a>stu[0].a)printf("A\n");elseprintf("B\n");//判断大小,无关问题if((p=fopen("d:\\tex.txt","w+"))==NULL)//打开文件printf("ERROR\n");else{for(i=0;i<2;i++){itoa(stu[i].a,temp,10);//转为字符写入fwrite(temp,sizeof(temp),1,p);fwrite(stu[i].b,sizeof(stu[i].b),1,p);}}//将结构体写入文件fclose(p);if((p=fopen("d:\\tex.txt","r"))==NULL)printf("ERROR\n");else{while((ch=fgetc(p))!=EOF)putchar(ch);fclose(p);}//输出文件信息}ps:读出来的时候也要考虑下,把他转为整形

⑤ 怎么把结构体的数据写到文件中

C语言把一个结构体数组写入文件分三步:1、以二进制写方式(wb)打开文件2、调用写入函数fwrite()将结构体数据写入文件3、关闭文件指针相应的,读文件也要与之匹配:1、以二进制读方式(rb)打开文件2、调用读文件函数fread()读取文件中的数据到结构体变量3、关闭文件指针参考代码如下:1234567891011121314151617181920212223242526272829303132333435363738394041424344 #include<stdio.h>struct stu { char name[30]; int age; double score;};int read_file();int write_file();int main(){ if ( write_file() < 0 ) //将结构体数据写入文件 return -1; read_file(); //读文件,并显示数据 return 0;}int write_file(){ FILE *fp=NULL; struct stu student={"zhang san", 18, 99.5}; fp=fopen( "stu.dat", "wb" ); //b表示以二进制方式打开文件 if( fp == NULL ) //打开文件失败,返回错误信息 { printf("open file for write error\n"); return -1; } fwrite( &student, sizeof(struct stu), 1, fp ); //向文件中写入数据 fclose(fp);//关闭文件 return 0;}int read_file(){ FILE *fp=NULL; struct stu student; fp=fopen( "stu.dat", "rb" );//b表示以二进制方式打开文件 if( fp == NULL ) //打开文件失败,返回错误信息 { printf("open file for read error\n"); return -1; } fread( &student, sizeof(struct stu), 1, fp ); //读文件中数据到结构体 printf("name=\"%s\" age=%d score=%.2lf\n", student.name, student.age, student.score ); //显示结构体中的数据 fclose(fp);//关闭文件 return 0;}

⑥ C语言中如何将已有结构体数据写入一个文件

用块写入 一次性把结构体的数据写入文件fwrite

⑦ C语言怎么把结构体写入文件

一般有两种方法.

structA{inta;floatf;chars[10];}m;

为例:

一种是写文本文件

以"w"打开

fprintf(fp,"%d%f%s",m.a,m.f,m.s);

另一种是写二进制文件.

以"wb"打开

fwrite(&m,sizeof(m),1,fp);


赞 (0)