C语言网

 找回密码
 加入社区!

QQ登录

只需一步,快速开始

查看: 499|回复: 2

自己写的一段简单的C语言代码  简单文件操作 [复制链接]

Rank: 6Rank: 6

主题
68
帖子
348
C币
578 枚
在线时间
57 小时
发表于 2010-7-9 06:55:26 |显示全部楼层
/*  显示模式主调用函数  */
/*  需要在main 函数中定义一个struct stud *header.

int display(void)
   {  
      char  yesno;
      char  *str;
      int   exist;
      short flag;
      extern struct stud *header;
      int show(const char *,struct stud **);
        
          printf("\t\t\tWhat file do you want to display?\n");
          pritnf("\t\t\tInput 0: Display last file.\n");
          printf("\t\t\tInput 1: Display a new file.\n")
          printf("\t\t\tInput Q or q to qiut display mode and back to main menu.\")
          printf("Please input:");

          yesno=getchar();
          for(;;)
            {
                switch(yesno)
                 {  
                    case: '0'
                        break;
                    case: '1'
                        break;
                    case: 'q'
                        return 1;
                    case: 'Q'
                        return 1;
                    default:
                        printf("input error,please input again.");
                 }
                 printf("\t\t\tWhat file do you want to display?\n");
                 pritnf("\t\t\tInput 0: Display last file.\n");
                 printf("\t\t\tInput 1: Display a new file.\n")
                 printf("\t\t\tInput Q or q to qiut display mode and back to main menu.\")
                 printf("Please input:");
                 yesno=getch();
            }
      
         
          if('0'==yesno)
            {
               exist=access("lastdisplay.dat",0);
               if(-1==exist)
                  {
                   printf("\nThere last view file do not exist.");
                   Printf("\nthen will back to main menu.")
                   return 1;
                   }
               else
                  {
                    show("lastdisplay.dat")
                    return 1;
                  }
            }
          else
            {
               printf("\nPlease input the file name.")
               getchar(str);
               exist=access(str,0)
               if(-1==exist)
                 {
                    printf("\nNot such a file or directry exist.")
                    Printf("\nthen will back to main menu").
                    return 1;
                 }
               else
                 {
                    flag=show("lastdisplay.dat",&header)
                    if(0==flag)
                        printf("\nthen will back to main menu.")
                    return 1;
                 }
            }   
     }

/* 数据显示函数  */
int show(const char *path,struct stud **temp)
  {
      int buffer;
      strrct stud *str1;
      struct stud *str2;
      long total;
      FILE *fp;
      
      buffer=sizeof(struct stud);
      total=0;
      str1=(struct *stud)malloc(sizeof(struct stud));
      str2=str1;
      if(NULL==(fp=fopen(path,"r")))
         {
         pfintf("\nthere have been some error.Can not open then file.");
         return 0;
         }
      fscanf(fp,"%s%d%c%d%s%d%d%s",&str1.name,&str1.number,&str1.sex,&str1.age,&str1.year,&str1.class,&str1.major,&str.tel);
      printf("\tname\t\tnumber\tsex\tage\tyear\t\tclass\tmajor\ttelephone");
      printf("\n%s%d%c%d%s%d%d%s", str1.name, str1.number, str1.sex, str1.age, str1.year, str1.class, str1.major, str.tel)
      *temp=NULL;
      while(!feof(fp))
        {
          total=total+1;
          if(1==total)
             *temp=str1;
          else
             str2.next=str1;
          str2=str1;
          str1=(struct *stud)malloc(sizeof(struct stud));
          fscanf(fp,"%s%d%c%d%s%d%d%s",&str1.name,&str1.number,&str1.sex,&str1.age,&str1.year,&str1.class,&str1.major,&str.tel);
          printf("\n%s%d%c%d%s%d%d%s", str1.name, str1.number, str1.sex, str1.age, str1.year, str1.class, str1.major, str.tel)      
        }
      str2.next=NULL;
      free(str2);
      free(str1);
      return 1;      
  }
2

查看全部评分

Rank: 1

主题
0
帖子
3
C币
3 枚
在线时间
0 小时
发表于 2010-8-11 08:14:34 |显示全部楼层
天书啊,头晕

Rank: 2

主题
2
帖子
80
C币
87 枚
在线时间
16 小时
发表于 2010-9-2 10:38:47 |显示全部楼层
注释呢?
您需要登录后才可以回帖 登录 | 加入社区!

C语言 ( 粤ICP备11042647号-2 )

GMT+8, 2012-2-7 22:22

©2009-2011 cyuyan.com.cn

回顶部