文件操作的奇怪?

来源:互联网 发布:傲视天地宝石进阶数据 编辑:程序博客网 时间:2024/05/17 04:32

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
 char *name;
 char *cellphone;
 char *birthday;
}stu[45];
void init(struct student *s,int n)
{
 int i;
 for(i=0;i<n;i++)
 {
  (s+i)->name=(char *)malloc(10);
  (s+i)->cellphone=(char *)malloc(11);
  (s+i)->birthday=(char *)malloc(5);
 }
}

void found1(void)
{
 int i;
 char *na=(char *)malloc(20);
 printf("please input the name:");
 gets(na);
 for(i=0;i<45;i++)
  if(0!=strcmp(stu[i].name,na))
   continue;
  else
  {
   printf("%s 's cellphone is :%s",na,stu[i].name);
   printf("%s 's birthday is :%s",na,stu[i].birthday);
   break;
  }
        if(45==i)
             printf("No this classmate!/n");
}

void found2(void)
{
 int i,j=0;
 char *birth=(char *)malloc(5);
 printf("please input the birthday like 07.07:/n");
 gets(birth);
 for (i=0;i<45;i++)
 if(strcmp(stu[i].birthday,birth)!=0)
       continue;
 else
     {
              printf("%s's birthday is :%s",stu[i].name,birth);
              j++;
            }
        if(0==j)
              printf("Nobody!/n");
}

void found3(void)
{
 int i;
 char *cellph=(char *)malloc(11);
 printf("please input the cellphone:/n");
 gets(cellph);
 for (i=0;i<45;i++)
   if(strcmp(stu[i].cellphone,cellph)!=0)
     continue;
   else
   {
  printf("%s's cellphone is :%s",stu[i].name,cellph);
  break;
   }
 if(i==45)
  printf("Nobady/n");
}

void found4(void)
{
 int i;
        FILE *p;
        struct student st[45];
 char *cellph=(char *)malloc(11), *na=(char *)malloc(20);
 printf("please input the name:/n");
 gets(na);
 printf("please input the cellphone:/n");
 gets(cellph);
        if((p=fopen("D://yy.dat","wb"))==NULL)
          {
               printf("open file error!/n");
               exit(0);
           }
 for (i=0;i<45;i++)
   if(strcmp(stu[i].name,na)!=0)
   continue;
   else if(i<45)
   {
    strcpy(stu[i].cellphone,cellph);
    rewind(p);
    for(i=0;i<45;i++)
    fwrite(&stu[i],sizeof(struct student),1,p);
                  break;
   }
   else printf(" input data error!/n");
        fclose(p);
}

int choose_interface(void)
{

 int j;
 printf("/n************************************/n");
 printf("***input 1 to find somebody   ******/n");
 printf("***input 2 to find birthday   ******/n");
 printf("***input 3 to find cellphone  ******/n");
 printf("***input 4 to chenge cellphone******/n");
 printf("***input 0 to exit            ******/n");
 printf("************************************/n");
 printf("please choose the number:");
 scanf("%d",&j);
 getchar();
 return j;
}
void main(void)
{
 int i,j;
        FILE *p;
 init(stu,45);
 if((p=fopen("D://yy.dat","rb"))==NULL)
 {
  printf("can't open file !/n");
  exit(0);
 }
        for(i=0;i<45;i++)
         fread(&stu[i],sizeof(struct student),1,p);
        fclose(p);
 for(i=0;i<45;i++)
 {
  if(i%5==0)
   putchar('/n');
  printf("%10s",stu[i].name);
 }
        j=choose_interface();
 while(j)
 {
     switch(j)
     {
         case 1: found1();
            break;
                case 2: found2();
            break;
         case 3: found3();
            break;
         case 4: found4();
            break;
         default: exit(0);
      }
     j=choose_interface();
 }
}
    其中的文件D://yy.dat是用这个程序创建的

#include <stdio.h>
struct student
{
 char *name;
 char *cellphone;
 char *birthday;
}stu[45]={
{"*****","******","****"},*****************//处于隐私我用*号代替
};
void main(void)
{
    FILE *p;
    int i;
    p=fopen("D://yy.dat","wb");
    for(i=0;i<45;i++)
     fwrite(&stu[i],sizeof(struct student),1,p);
    fclose(p);
 printf("well done!!/n");
}
但最后输出全市乱码!

原创粉丝点击