⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exp10_01.c

📁 《C语言程序设计教程、实验与练习》 源文件下载
💻 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 + -