fread fwrite实例

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


struct user 
{
char name[20];
char passwd[20];
};

int gen();
int main()
{
gen();
return 0;
}
int gen()
{
struct user *p;
FILE *fp;
p=malloc(sizeof(struct user));


if ((fp = fopen("admin.txt", "rw+")) == NULL) {
printf("the file doesnot exist");  
return 0; //failure
}
scanf("%s",p->name);
scanf("%s",p->passwd);
printf("\nname is%s",p->name);
printf("\npasswd is%s",p->passwd);
//fwrite(p, 1, sizeof(struct user), fp);
fwrite(p, sizeof(struct user), 1, fp);
memset(p, ' ', sizeof(struct user) );


fseek(fp, 0, SEEK_SET); 
fread(p, sizeof(struct user), 1, fp);
printf("after read\n");
printf("\nname is%s",p->name);
printf("\npasswd is%s",p->passwd);
fclose(fp);


}

//红色的三处,哎呀,容易忽视哦~~~大哭

你可能感兴趣的:(struct,user,File,null,FP)