#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct info
{
char xuehao[15];
char xingming[20];
char xingbie[5];
char qq[10];
char phone[13];
char e_mail[30];
char messege[50];
};
void main()
{
struct info *pointor;
FILE *fp;
char change[10];
char flag='N';
char filename[50];
void getinfo(struct info *);
void printinfo(struct info*);
void input(char *buffer);
FILE *creat(char *);
void save(struct info*,int size,int count,FILE *fp);
void read(struct info*pointor,int size,int count,FILE *fp);
/*******************************************************************/
printf("Enter the file name:\n");
input(filename);
fp=creat(filename);
printf("Enter data:\n");
do
{
pointor=(struct info*)malloc(sizeof(struct info));
getinfo(pointor);
save(pointor,sizeof(struct info),1,fp);
printf("continue?(YES\\NO)\n");
input(change);
if(strcmp("yes",change)==0)
flag='Y';
else
flag='N';
}
while(flag=='Y');
/******************************************************************/
printf("to open file:\n");
printf("Enter the filename:\n");
input(filename);
fp=fopen(filename,"r");
do
{
read(pointor,sizeof(struct info),1,fp);
printinfo(pointor);
printf("continue:(yes\\no)\n");
input(change);
if(strcmp("yes",change)==0)
flag='Y';
else
flag='N';
}
while(flag=='Y');
/*****************************************************************/
}
void getinfo(struct info *pointor)
{
printf("xuehao:");
input(pointor->xuehao);
printf("xingming:");
input(pointor->xingming);
printf("xingbie:");
input(pointor->xingbie);
printf("qq:");
input(pointor->qq);
printf("phone:");
input(pointor->phone);
printf("e-mail:");
input(pointor->e_mail);
printf("messege:");
input(pointor->messege);
}
void printinfo(struct info *pointor)
{
printf("xuehao:");
printf("%s\n",pointor->xuehao);
printf("xingming:");
printf("%s\n",pointor->xingming);
printf("xingbie:");
printf("%s\n",pointor->xingbie);
printf("qq:");
printf("%s\n",pointor->qq);
printf("phone:");
printf("%s\n",pointor->phone);
printf("e-mail:");
printf("%s\n",pointor->e_mail);
printf("messege:");
printf("%s\n",pointor->messege);
}
void input(char *buffer)
{
char c;
char *p;
p=buffer;
c=getchar();
while(c!='\n')
{
*p=c;
p++;
c=getchar();
}
*p='\0';
}
FILE *creat(char *filename)
{
FILE *fp;
fp=fopen(filename,"w");
if(fp!=NULL)
printf("creat ok!\n");
else
printf("creat error!\n");
return fp;
}
void save(struct info*pointor,int size,int count,FILE *fp)
{
fwrite(pointor,size,count,fp);
printf("save ok!\n");
}
void read(struct info*pointor,int size,int count,FILE *fp)
{
fread(pointor,size,count,fp);
printf("read ok!\n");
}