本文主要是介绍真题五十二、五十三、五十四,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//将每个人信息作为数据块写入到二进制文件中
#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef struct
{ int num;char name[10];char tel[10];
}STYPE;
void check();/**********found**********/
int fun(STYPE *std)//函数形参为结构体类型,前面有指明
{
/**********found**********/FILE *fp; int i;//文件定义是FILEif((fp=fopen("myfile5.dat","wb"))==NULL)return(0);printf("\nOutput data to file !\n");for(i=0; i<N; i++)
/**********found**********/fwrite(&std[i], sizeof(STYPE), 1, fp);//函数调用的形式fwrite(buffer,size,count,fp)fclose(fp);return (1);
}
void main()
{ STYPE s[10]={ {1,"aaaaa","111111"},{1,"bbbbb","222222"},{1,"ccccc","333333"},{1,"ddddd","444444"},{1,"eeeee","555555"}};int k;k=fun(s);if (k==1){ printf("Succeed!"); check(); }elseprintf("Fail!");
}
void check()
{ FILE *fp; int i;STYPE s[10];if((fp=fopen("myfile5.dat","rb"))==NULL){ printf("Fail !!\n"); exit(0); }printf("\nRead file and output to screen :\n");printf("\n num name tel\n");for(i=0; i<N; i++){ fread(&s[i],sizeof(STYPE),1, fp);printf("%6d %s %s\n",s[i].num,s[i].name,s[i].tel);}fclose(fp);
}
这篇关于真题五十二、五十三、五十四的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!