文件综合测试
1 对C文件操作有些什么特点?什么是缓冲文件系统?什么是非缓冲文件系统?这二者
     的缓冲区有什么区别?
解: 略.
2 什么是文件型指针?通过文件指针访问文件有什么好处?
解: 略.
3 对文件的打开和关闭的含义是什么?为什么要打开和关闭文件?
解:略.
4 从键盘输入一个字符串,将其中的小写字母全部转化成大写字母,然后输出到一个
     键盘文件"test"中保存.输入的字符串以"!"结束.
 解:
#include"stdio.h"
main()
{ FILE *fp;
  char str[100];
  int i=0;
  if((fp=fopen("test","w"))==NULL)
   { printf("can not open the file\n");
     exit(0);
   }
  printf("input a string:\n");
  gets(str);
  while(str[i]!='!')
   { if(str[i]>='a'&&str[i]<='z')
       str[i]=str[i]-32;
     fputc(str[i],fp);
     i++;
   }
  fclose(fp);
  fp=fopen("test","r");
  fgets(str,strlen(str)+1,fp);
  printf("%s\n",str);
  fclose(fp);
}
5 有两个磁盘文件"A"和"B",各自存放一行字母,要求把这两个文件中的信息合并(按字
     顺序排列,输出到一个新文件"C"中.
解:
#include"stdio.h"
main()
{ FILE *fp;
  int i,j,n,i1;
  char c[100],t,ch;
  if((fp=fopen("a1","r"))==NULL)
    { printf("can not open the file\n");
     exit(0);
   }
  printf("\n file A:\n");
  for(i=0;(ch=fgetc(fp))!=EOF;i++)
   { c[i]=ch;
     putchar(c[i]);
   }
  fclose(fp);
  i1=i;
  if((fp=fopen("b1","r")==NULL)
   { printf("\ncan not open the file");
     exit(0);
   }
  printf("\n file B:\n");
  for(i=i1;(ch=fgetc(fp))!=EOF;i++)
   { c[i]=ch;
     putchar(c[i]);
   }
  fclose(fP);
  n=i;
  for(i=0;i<n;i++)
   for(j=i+1;j<n;j++)
    if(c[i]>c[j])
   { t=c[i];
     c[i]=c[j];
     c[j]=t;
   }
  printf("\n file C:\n");
  fp=fopen("c1","w");
   for(i=0;i<n;i++)
    { putc(c[i],fp);
      putchar(c[i]);  
    }
  fclose(fp);
}
6 有5个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生学号,姓名,三
     门课的成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中
解:
#include"stdio.h"
struct student
{ char num[10];
  char name[8];
  int  score[3];
  float ave;
} stu[5];
main()
{ int i,j,sum;
  FILE *fp;
  for(i=0;i<5;i++)
   { printf("\ninput score of student %d:\n",i+1);
     printf(NO.:");
     scanf("%s",stu[i].num);
     printf("name:");
     scanf("%s",stu[i].name); 
     sum=0; 
    for(j=0;j<3;j++)
     { printf("score %d:",j+1);
       scanf("%d",&stu[i].score[j]);
       sum+=stu[i].score[j];
     }
    stu[i].ave=sum/3.0;
   }
  fp=fopen("stu","w");
  for(i=0;i<5;i++)
   if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
     printf("file write error\n");
  fclose(fp);
  fp=fopen("stud","r");
  for(i=0;i<5;i++)
   { fread(&stud[i],sizeof(struct student),1,fp);
     printf("%s,%s,%d,%d,%d,%6.2f\n",stud[i].num,stud[i].name,stud[i].score[0]
             stud[i].score[1],stud[i].score[2],stud[i].ave;
   }
}
7 将上题stud文件中的学生数据按平均分进行排序处理,并将已排序的学生数据存入
     一个新文件stu_sort中
解:
#include"stdio.h"
#define N 10
struct student
{ char num[10];
  char name[8];
  int  score[3];
  float ave;
} st[N],temp;
main()
{ FILE *fp;
  int i,j,n;
  if((fp=fopen("stud","r")==NULL)
   { printf("\ncan not open the file");
     exit(0);
   }
  printf("\nfile'stud':");
  for(i=0;fread(&st[i],sizeof(struct student),1,fp)!=0;i++)
   { printf("\n%8s%8s",st[i].num,st[i].name);
     for(j=0;j<3;j++)
     printf("%8d",st[i].score[j]);
     printf("%10.2f",st[i].ave);
   }
  fclose(fp);
  n=i;
  for(i=0;i<n;i++)
   for(j=i+1;j<n;j++)
    if(st[i].ave<st[j].ave)
     { temp=st[i];
       st[i]=st[j];
       st[j]=temp;
     }
  printf("\nnow:");
  fp=fopen("stu_sort","w");
  for(i=0;i<n;i++)
   { fwrite(&st[i],sizeof(struct student),1,fp);
     printf("\n%8s%8s",st[i].num,st[i].name);
     for(j=0;j<3;j++)
      printf("%8d",st[i].score[j]);
     printf("%10.2f",st[i].ave);
   }
 }
 fclose(fp);
}
8 将上题已经排好序的学生成绩文件插入处理,插入一个学生的3门课成绩,程序先计算
     新插入学生的平均成绩,然后将它按平均成绩高低顺序插入,插入后建立一个新的文件
解:
#include"stdio.h"
struct student
{ char num[10];
  char name[8];
  int  score[3];
  float ave;
} st[10],s;
main()
{ FILE *fp,*fp1;
  int i,j,t,n;
  printf("\nNO.:");
  scanf("%s",s.num);
  printf("name:");
  scanf("%s",s.name);
  printf("score1,score2,score3:");
  scanf("%d,%d,%d",&s.score[o],&s.score[1],&s.score[2]);
  s.ave=(s.score[o]+s.score[1]+s.score[2])/3.0;
  if((fp=fopen("stu_sort","r")==NULL)
   { printf("\ncan not open the file");
     exit(0);
   }
  printf("original data:\n");
  for(i=0;fread(&st[i],sizeof(struct student),1,fp)!=0);i++)
   { printf("\n%8s%8s",st[i].num,st[i].name);
     for(j=0;j<3;j++)
      printf("%8d",st[i].score[j]);
     printf("%10.2f",st[i].ave);
   }
  n=i;
  for(t=0;st[t].ave>s.ave&&t<n;t++);
  printf("now:");
  fp1=fopen("sort1.dat","w");
  for(i=0;i<t;i++)
   { fwrite(&st[i],sizeof(struct student),1,fp);
     printf("\n%8s%8s",st[i].num,st[i].name);
     for(j=0;j<3;j++)
      printf("%8d",st[i].score[j]);
     printf("%10.2f",st[i].ave);
   }
  fclose(fp);
  fclose(fp1);
}
9 上题结果仍存入原有的stu_sort文件而不另建立新文件.
解:
#include"stdio.h"
struct student
{ char num[10];
  char name[8];
  int  score[3];
  float ave;
} st[10],s;
main()
{ FILE *fp,*fp1;
  int i,j,t,n;
  printf("\nNO.:");
  scanf("%s",s.num);
  printf("name:");
  scanf("%s",s.name);
  printf("score1,score2,score3:");
  scanf("%d,%d,%d",&s.score[o],&s.score[1],&s.score[2]);
  s.ave=(s.score[o]+s.score[1]+s.score[2])/3.0;
    if((fp=fopen("stu_sort","r")==NULL)
   { printf("\ncan not open the file");
     exit(0);
   }
  printf("original data:\n");
  for(i=0;fread(&st[i],sizeof(struct student),1,fp)!=0);i++)
   { printf("\n%8s%8s",st[i].num,st[i].name);
     for(j=0;j<3;j++)
      printf("%8d",st[i].score[j]);
     printf("%10.2f",st[i].ave);
   }
  fclose(fp);
  n=i;
  for(t=0;st[t].ave>s.ave&&t<n;t++);
  printf("\nnow:\n");
  fp=fopen("stu_sort","w");
  for(i=0;i<t;i++)
   { fwrite(&st[i],sizeof(struct student),1,fp);
     printf("\n%8s%8s",st[i].num,st[i].name);
     for(j=0;j<3;j++)
      printf("%8d",st[i].score[j]);
     printf("%10.2f",st[i].ave);
   }
  fwrite(&s,sizeof(struct student),1,fp);
  printf("\n%9s%8s%8d%8d%10.2f",s.num,s.name,s.score[0],s.score[1],s.score[2]
          ,s.ave);
  for(i=t;i<n;i++)
   { fwrite(&st[i],sizeof(struct student),1,fp);
     printf("\n%8s%8s",st[i].num,st[i].name);
     for(j=0;j<3;j++)
      printf("%8d",st[i].score[j]);
     printf("%10.2f",st[i].ave);
   }
  fclose(fp);
}
10 有一磁盘文件emploee,内存放职工的数据,每个职工的数据包括:职工的姓名,性别
      工号,年龄,住址,工资,健康状况,文化程度.要求将职工名和工资的信息单独抽出来
      建立一个简明的职工工资文件.
解:
#include"stdio.h"
struct emploee
{ char num[6];
  char name[10];
  char sex[2];
  int  age;
  char addr[20];
  int  salary;
  char health[8];
  char class[10];
}em[10];
struct em[10]
{ char name[10];
  int salary;
}em_case[10];
main()
{ FILE *fp1,*fp2;
  int i,j;
  if((fp1=fopen("emploee","r"))==NULL)
   { printf("\ncan not open the file");
     exit(0);
   }
  printf("\n NO. name sex age addr salary health class\n");
  for(i=0;fread(&em[i],sizeof(struct emploee),1,fp1)!=0);i++)
   { printf("\n%4s%8s%4s%6d%10s%6d%10s%8s",em[i].num,em[i].name,em[i].sex,
             em[i].age,em[i].addr,em[i].salary,em[i].health,em[i].class);
     strcpy(em_case[i].name,em[i].name);
     em_case[i].salary=em[i].salary;
   }
  printf("\n\n* * * * * * * * * * * * * * * * * * * * * * * * *  ");
  if((fp2=fopen("emp_salary","wb"))==NULL)
   { printf("\ncan not open the file");
     exit(0);
   }
  for(j=0;j<i;j++)
   { if(fwrite(&em_case[j],sizeof(struct emp),1,fp2)!=1);
      printf("error");
     printf("\n %12s%10d",em_case[j].name,em_case[j].salary);
   }
  printf("\n\n* * * * * * * * * * * * * * * * * * * * * * * * *  ");
  fclose(fp1);
  fclose(fp2);
}
  说明:数据文件emploee是事先建立好的,其中已有职工数据,而文件则是由程序建立的.
  建立emploee文件的程序如下:
#include"stdio.h"
struct emploee
{ char num[6];
  char name[10];
  char sex[2];
  int  age;
  char addr[20];
  int  salary;
  char health[8];
  char class[10];
}em[10];
main()
{ FILE *fp1,*fp2;
  int i,j;
  printf("\n NO. name sex age addr salary health class\n");  
  for(i=0;i<4;i++)
   scanf("%s%s%s%d%s%d%s%s",em[i].num,em[i].name,em[i].sex,
             &em[i].age,em[i].addr,&em[i].salary,em[i].health,em[i].class);
  if((fp=fopen("emploee","w"))==NULL)
   { printf("\ncan not open the file");
     exit(0);
   } 
  for(i=0;i<4;i++)
   if(fwrite(&em[i],sizeof(struct emploee),1,fp)!=1);
      printf("error\n");
  fclose(fp);
}
11 从上题的"职工工资文件"中删去一个职工的数据,再存回原文件.
解:
#include"stdio.h"
#include"string.h"
struct emploee
{ char name[10];
  int  salary;
}emp[20];
main()
{ FILE *fp;
  int i,j,n,flag;
  char name[10];
  int  salary;
  if((fp=fopen("emp_salary","rb"))==NULL)
   { printf("\ncan not open the file");
     exit(0);
   }
  printf("\n original data:");
  for(i=0;fread(&emp[i],sizeof(struct emploee),1,fp)!=0);i++)
    printf("\n %8s%7d,emp[i].name,emp[i].salary);
  fclose(fp);
  n=i;
  printf("\nInput name deleted:");
  scanf("%s",name);
  for(flag=1,i=0;flag&&i<n;i++)
   { if(strcmp(name,emp[i].name)==0)
      { for(j=i;j<n-1;j++)
          {  strcpy(emp[j].name,emp[j+1].name);
             emp[j].salary=emp[j+1].salary;
          }
         flag=0;
      }
   }
  if(!flag)
    n=n-1;
  else
    printf("\nNot found!");
  printf("\nNow,the content of file:\n");
  fp=fopen("emp_salary","wb");
  for(i=0;i<n;i++)
    fwrite(&emp[i],sizeof(struct emploee),1,fp);
  fclose(fp);
  fp=fopen("emp_salary","r");
  for(i=0;fread(&emp[i],sizeof(struct emploee),1,fp)!=0);i++)
    printf("\n %8s%7d,emp[i].name,emp[i].salary);
  fclose(fp);
}
12 从键盘输入若干字符(每行长度不等),输入后把它们存放到一磁盘文件中.再从该
      文件中读入这些数据,将其中小写字母转换成大写字母后在显示屏上输出.
解:
#include"stdio.h"
main()
{ int i,flag;
  char str[80],c;
  FILE *fp;
  fp=fopen("text","w");
  flag=1;
  while(flag==1)
   { printf("\n input string:\n");
     gets(str);
     fprintf(fp,"%s",str);
     printf("\nContinue?");
     c=getchar();
     if((c=='N')||(c=='n'))
       flag=0;
     getchar();
   }
  fclose(fp);
  fp=fopen("text","r");
  while(fscanf(fp,"%s",str)!=EOF)
   { for(i=0;str[i]!='\0';i++)
      if((str[i]>='a')&&(str[i]<='z'))
        str[i]-=32;
     printf("\n%s\n",str);
   }
  fclose(fp);
}