📄 exp10_01.c
字号:
#include <stdio.h>
#include <alloc.h>
#include <string.h>
struct user_info
{
char name[20];
int age;
char phone[20];
char address[80];
};
typedef struct user_info USERINFO;
void getfilepathname(char *path,char *filetile)
{
int i;
int len;
len=strlen(path);
for(i=len-1;i>=0;i--)
if(path[i]=='\\')
break;
strcpy(path+i+1,filetile);
}
void main(int argc,char *argv[])
{
int i,num;
USERINFO *puser;
char filename[80];
FILE *fp;
strcpy(filename,argv[0]);
getfilepathname(filename,"user.dat");
printf("input the number of users:");
scanf("%d",&num);
fflush(stdin);
puser=(USERINFO *)malloc(num*sizeof(USERINFO));
if(puser==NULL)
{
printf("no enough memory\n");
return;
}
for(i=0;i<num;i++)
{
printf("input the name of the %dth user:\n",i+1);
gets(puser[i].name);
fflush(stdin);
printf("input the age of the %dth user:\n",i+1);
scanf("%d",&puser[i].age);
fflush(stdin);
printf("input the phone of the %dth user:\n",i+1);
gets(puser[i].phone);
fflush(stdin);
printf("input the address of the %dth user:\n",i+1);
gets(puser[i].address);
fflush(stdin);
}
fp=fopen(filename,"wb");
if(fp==NULL)
{
printf("can't creat user.dat\n");
free(puser);
return;
}
fprintf(fp,"%d",num);
fwrite(puser,sizeof(USERINFO),num,fp);
fclose(fp);
free(puser);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -