第十三章
例13.1 程序为:
#include
#include
void main()
{
FILE *fp;
char ch,filename[10];
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
ch=getchar();
ch=getchar();
while(ch!='#')
{
fputc(ch,fp);putchar(ch);
ch=getchar();
}
putchar(10);
fclose(fp);
}
例13.2 程序为:
#include
#include
void main()
{
FILE *in, *out;
char ch,infile[10],outfile[10];
printf("Enter the infile name:\n");
scanf("%s",infile);
printf("Enter the infile name:\n");
scanf("%s",outfile);
if((in=fopen(infile,"r"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
if((out=fopen(outfile,"w"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
while(!feof(in)) fputc(fgetc(in),out);
fclose(in);
fclose(out);
}
注:在该程序的运行中,每复制一次就会产生一个乱码置于最后,并依次叠加。这不是个优秀的程序。
13.5.1 rewind函数:
#include
void main()
{
FILE *fp1,*fp2;
fp1=fopen("file1.c","r");
fp2=fopen("file2.c","w");
while(!feof(fp1)) putchar(getc(fp1));
rewind(fp1);
while(!feof(fp1)) putc(getc(fp1),fp2);
fclose(fp1);fclose(fp2);
}
例13.5
#include
#include
struct student_type
{
char name[10];
int num;
int age;
char sex;
}stud[10];
void main()
{
int i;
FILE *fp;
if((fp=fopen("file22.c","rb"))==NULL)
{
printf("can not open file\n");
exit(0);
}
for(i=0;i<10;i+=2)
{
fseek(fp,i*sizeof(struct student_type),0);
fread(&stud[i],sizeof(struct student_type),1,fp);
printf("%s %d %d %c\n",stud[i].name,stud[i].num,stud[i].age,stud[i].sex);
}
fclose(fp);
}
注:这个程序是对的,但常用于处理二进制文件,因为文本文件要发生字符转换,计算位置时常发生混乱。
习题
13.1 对c文件操作有些什么特点?什么是缓冲文件系统?什么是非缓冲文件系统?这两者的缓冲区有什么区别?
答:c语言把文件看作是一个字符的序列级,即由一个一个字符的数据顺序组成。根据数据的组织形式,可以分为:ASCII文件(文本文件)和二进制文件。一个c文件是一个字节流或二进制流(流式文件)。它允许对文件存取一个字符,这就增加了处理的灵活性。
缓冲文件系统:是指系统自动在内存中为每一个正在使用的文件开辟一个缓冲区,如果从磁盘向内存读入数据,则一次从磁盘文件将一批数据输入到内存缓冲区,然后再从缓冲区逐个地将数据送到程序数据区中去。
非缓冲文件系统:是指系统不自动开辟确定大小的缓冲区,而有程序为每个文件设定缓冲区。用缓冲文件系统进行的输入输出又称高级磁盘输入输出,用非缓冲文件系统进行的输入输出又称为低级输出输入系统。ANSI C标准不采用非缓冲文件系统,而只采用缓冲文件系统,即既用缓冲文件系统处理文本文件,也用它处理二进制文件,也就是将缓冲文件系统扩充为可以处理二进制文件。
13.2 什么是文件型指针?通过文件型指针访问文件有什么好处?
答:缓冲文件系统中,关键的概念是“文件指针”。每个被使用的文件都在内存中开辟一个区,用来存放文件的有关信息(如文件的名字,文件的状态及文件当前位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是由系统定义的,取名为FILE。这和一般使用指针变量的好处是一样的。
13.3 对文件的打开和关闭的含义是什么?为什么要打开和关闭文件?
答:打开的含义:带回指向a1文件的指针赋给fp,这样fp就和文件a1联系上了。或者说,fp指向a1文件。在使用完一个文件后应该关闭它,以防止它再被误用。“关闭”就是文件指针变量不指向该文件,也就是文件指针变量与文件“脱钩”,此后不能再通过该指针对原来与其联系的文件进行读写操作,除非再次打开,使该指针变量重新指向该文件。
13.4 程序为:
#include
#include
void main()
{
FILE *fp;
char ch,filename[10];
printf("please input the name of the file:\n");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
printf("please a string:\n");
ch=getchar();
if('a'<=ch&&ch<='z')
ch=ch-32;
while(ch!='!')
{
fputc(ch,fp);putchar(ch);
ch=getchar();
if('a'<=ch&&ch<='z')
ch=ch-32;
}
putchar(10);
fclose(fp);
}
13.5 程序为:
#include
#include
#include
void main()
{
FILE *fp;
char a[100];
if((fp=fopen("test.c","r"))==NULL)
{
printf("文件打开错误");
exit(0);
}
int i=0;
a[i]=fgetc(fp);
while(a[i]!=EOF)
{
putchar(a[i]);
i++;
a[i]=fgetc(fp);
}
putchar('\n');
fclose(fp);
if((fp=fopen("test1.c","r"))==NULL)
{
printf("文件打开错误");
exit(0);
}
while(a[i]!=EOF)
{
putchar(a[i]);
i++;
a[i]=fgetc(fp);
}
a[i]='\0';
putchar('\n');
fclose(fp);
int n=i;
int k;
char t;
for(i=0;i { k=i; for(int j=i+1;j { if(a[k]>a[j]) k=j; } if(k!=i) { t=a[i]; a[i]=a[k]; a[k]=t; } } if((fp=fopen("test2.c","w"))==NULL) { printf("文件打开错误"); exit(0); } i=0; while(i { fputc(a[i],fp); putchar(a[i]); i++; } fclose(fp); putchar('\n'); } 注:这个程序将处理数组的排序问题和文件读取的问题结合起来了。从而解决了这个题目。 13.6 程序为:(这个程序不难,自己编的,对其中文件类型和它和结构体数组之间信息的转换有了较深的认识) #include #define SIZE 5 struct student_type { int num; char name[10]; float score1; float score2; float score3; float avg; }stud[SIZE]; void save() { FILE *fp; int i; if((fp=fopen("stu_list.c","wb"))==NULL) { printf("cannot open file\n"); return; } for(i=0;i if(fwrite(&stud[i],sizeof(student_type),1,fp)!=1) printf("file write error"); fclose(fp); } void main() { FILE *fp; int i; for(i=0;i { scanf("%d%s%f%f%f",&stud[i].num,stud[i].name,&stud[i].score1,&stud[i].score2,&stud[i].s core3); stud[i].avg=(stud[i].score1+stud[i].score2+stud[i].score3)/3; } save(); fp=fopen("stu_list.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); printf("%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2f\n",stud[i].num,stud[i].name,stud[i].sc ore1,stud[i].score2,stud[i].score3,stud[i].avg); } } 13.7 程序为: #include #define SIZE 5 struct student_type { int num; char name[10]; float score1; float score2; float score3; float avg; }stud[SIZE]; void save() { FILE *fp; int i; if((fp=fopen("stu_sort.c","wb"))==NULL) { printf("cannot open file\n"); return; } for(i=0;i if(fwrite(&stud[i],sizeof(student_type),1,fp)!=1) printf("file write error"); fclose(fp); } void main() { FILE *fp; int i,j; struct student_type temp; fp=fopen("stu_list.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); } for(i=0;i for(j=i+1;j { if(stud[i].avg>stud[j].avg) { temp=stud[i]; stud[i]=stud[j]; stud[j]=temp; } } save(); fp=fopen("stu_sort.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); printf("%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2f\n",stud[i].num,stud[i].name,stud[i].sc ore1,stud[i].score2,stud[i].score3,stud[i].avg); } } 13.9程序为:(这个题的程序要建立在前面那些程序的基础之上) #include #define SIZE 5 struct student_type { int num; char name[10]; float score1; float score2; float score3; float avg; }stud[SIZE+1]; void save() { FILE *fp; int i; if((fp=fopen("stu_sort.c","wb"))==NULL) { printf("cannot open file\n"); return; } for(i=0;i if(fwrite(&stud[i],sizeof(student_type),1,fp)!=1) printf("file write error"); fclose(fp); } void main() { FILE *fp; int i,j; struct student_type temp,another; scanf("%d%s%f%f%f",&another.num,https://www.doczj.com/doc/2114979114.html,,&another.score1,&another.score2,&another.s core3); another.avg=(another.score1+another.score2+another.score3)/3; fp=fopen("stu_list.c","ab"); fwrite(&another,sizeof(student_type),1,fp); fclose(fp); fp=fopen("stu_list.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); } for(i=0;i for(j=i+1;j { if(stud[i].avg>stud[j].avg) { temp=stud[i]; stud[i]=stud[j]; stud[j]=temp; } } fclose(fp); save(); fp=fopen("stu_sort.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); printf("%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2f\n",stud[i].num,stud[i].name,stud[i].sc ore1,stud[i].score2,stud[i].score3,stud[i].avg); } fclose(fp); } 13.8 程序为:(由上题易得该程序,这里建立的新文件的名字为:students.c) #include #define SIZE 5 struct student_type { int num; char name[10]; float score1; float score2; float score3; float avg; }stud[SIZE+1]; void save() { FILE *fp; int i; if((fp=fopen("students.c","wb"))==NULL) { printf("cannot open file\n"); return; } for(i=0;i if(fwrite(&stud[i],sizeof(student_type),1,fp)!=1) printf("file write error"); fclose(fp); } void main() { FILE *fp; int i,j; struct student_type temp,another; scanf("%d%s%f%f%f",&another.num,https://www.doczj.com/doc/2114979114.html,,&another.score1,&another.score2,&another.s core3); another.avg=(another.score1+another.score2+another.score3)/3; fp=fopen("stu_list.c","ab"); fwrite(&another,sizeof(student_type),1,fp); fclose(fp); fp=fopen("stu_list.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); } for(i=0;i for(j=i+1;j { if(stud[i].avg>stud[j].avg) { temp=stud[i]; stud[i]=stud[j]; stud[j]=temp; } } fclose(fp); save(); fp=fopen("students.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); printf("%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2f\n",stud[i].num,stud[i].name,stud[i].sc ore1,stud[i].score2,stud[i].score3,stud[i].avg); } fclose(fp); } 13.10 程序为: #include #define SIZE 1 struct employee_type { char name[10]; int num; int age; char sex[5]; char health[10]; int salary; char adress[10]; char edu[10]; }emp[SIZE]; struct wage_type { char name[10]; int salary; }emp1[SIZE]; void save() { FILE *fp; int i; if((fp=fopen("employee_list.c","wb"))==NULL) { printf("cannot open file\n"); return; } for(i=0;i if(fwrite(&emp[i],sizeof(employee_type),1,fp)!=1) printf("file write error"); fclose(fp); } void save1() { FILE *fp; int i; if((fp=fopen("employee_list1.c","wb"))==NULL) { printf("cannot open file\n"); return; } for(i=0;i if(fwrite(&emp1[i],sizeof(wage_type),1,fp)!=1) printf("file write error"); fclose(fp); } void main() { FILE *fp; int i; for(i=0;i { scanf("%s%d%d%d%s%s%s%s",emp[i].name,&emp[i].num,&emp[i].age,&emp[i].salary,em p[i].adress,emp[i].edu,emp[i].health,emp[i].sex); printf("%s %d %d %d %s %s %s %s\n",emp[i].name,emp[i].num,emp[i].age,emp[i].salary,e mp[i].adress,emp[i].edu,emp[i].health,emp[i].sex); } save(); fp=fopen("employee_list.c","rb"); for(i=0;i { fread(&emp[i],sizeof(employee_type),1,fp); for(int j=0;j<10;j++) emp1[i].name[j]=emp[i].name[j]; emp1[i].salary=emp[i].salary; } save1(); for(i=0;i { fread(&emp1[i],sizeof(wage_type),1,fp); printf("%s %d\n",emp1[i].name,emp1[i].salary); } } 注意:这个程序的编写不难,主要是在于用scanf函数读入数据时的一些细节问题。 13.11 这个程序在上题的基础上比较简单,只需在从文件把数据到数组时,删掉一个即可(略)。 13.12 程序为: #include void main() { int i,flag; char str[80],c; FILE *fp; fp=fopen("text","w"); flag=1; while(flag==1) { printf("Input string:\n"); gets(str); fprintf(fp,"%s",str); printf("Continue?"); 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); } 注:这个程序很简单,不用费多大功夫,只需关注这个程序的风格就行了。 一个笔试中常见的程序: #include using namespace std; int main() { int count=1; label: //标记label标签 cout< if(count<=100) goto label;//如果count的值不大于100则转到1abel标签处开始执行程序cout< return 0; } 注意:整个程序中没有for,while,do...wihle语句,但却实现了循环。 用C++编写程序,完成从键盘录入学生成绩的同时,计算班级平均成绩和及格学生平均成绩。程序为: #include int main() { float score[10]; float sum1=0; float sum2=0; float avg1,avg2,count1=0; for(int i = 0;i < 10;i++) { cout<<"请输入学生成绩:"< cin>>score[i]; sum1+=score[i]; if (score[i]<60 ) //若score[i]不及格则退出本次循环 continue; sum2+=score[i]; count1+=1; } avg1 = sum1/10; avg2 = sum2/count1; cout<<"班级学生平均成绩为:"< cout<<"及格学生平均成绩为:"< return 0; } 自己编的一个程序: #include int main() { int i,renshu,n=0,m=0; float sum1=0,sum2=0,score,aver1,aver2; cout<<"请输入学生人数:"; cin>>renshu; cout<<"请依次输入学生成绩:"< for(i=0;i { cin>>score; if(score>=60) { sum1+=score; n++; } sum2+=score; m++; } aver1=sum1/n; aver2=sum2/m;