C程序设计第四版谭浩强习题例题第10章.docx
《C程序设计第四版谭浩强习题例题第10章.docx》由会员分享,可在线阅读,更多相关《C程序设计第四版谭浩强习题例题第10章.docx(28页珍藏版)》请在冰豆网上搜索。
C程序设计第四版谭浩强习题例题第10章
第10章
例10.1
#include
#include
intmain()
{
FILE*fp;
charch,filename[10];
printf("请输入所用的文件名:
");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("无法打开此文件\n");
exit(0);//终止程序
}
ch=getchar();//接收最后输入的回车
printf("请输入一个准备存储到磁盘的字符串(以#结束):
");
ch=getchar();
while(ch!
='#')
{
fputc(ch,fp);
putchar(ch);//输出字符显示在屏幕上
ch=getchar();
}
fclose(fp);
putchar(10);//'\n'=10
return0;
}
例10.2
#include
#include
intmain()
{
FILE*in,*out;
charch,infile[10],outfile[10];
printf("请输入读入的文件名:
");
scanf("%s",infile);
printf("请输入输出的文件名:
");
scanf("%s",outfile);
if((in=fopen(infile,"r"))==NULL)
{
printf("无法打开此文件\n");
exit(0);//终止程序
}
if((out=fopen(outfile,"w"))==NULL)
{
printf("无法打开此文件\n");
exit(0);//终止程序
}
while(!
feof(in))
{
ch=fgetc(in);
fputc(ch,out);
putchar(ch);
}
putchar(10);
fclose(in);
fclose(out);
return0;
}
例10.3
#include
#include
#include
intmain()
{
FILE*fp;
charstr[3][10],temp[10];
inti,j,k,n=3;
printf("Enterstrings:
\n");
for(i=0;igets(str[i]);
for(i=0;i{
k=i;
for(j=i+1;jif(strcmp(str[k],str[j])>0)k=j;
if(k!
=i)
{
strcpy(temp,str[i]);
strcpy(str[i],str[k]);
strcpy(str[k],temp);
}
}
if((fp=fopen("D:
\\360data\\重要数据\\我的文档\\string.dat","w"))==NULL)
{
printf("can'topenthefile!
\n");
exit(0);
}
printf("\nThenewsequence:
\n");
for(i=0;i{
fputs(str[i],fp);
fputs("\n",fp);
printf("%s\n",str[i]);
}
return0;
}
#include
#include
#include
intmain()
{
FILE*fp;
charstr[3][10];
inti=0;
if((fp=fopen("D:
\\360data\\重要数据\\我的文档\\string.dat","r"))==NULL)
{
printf("can'topenthefile!
\n");
exit(0);
}
while(fgets(str[i],10,fp)!
=NULL)
{
printf("%s",str[i]);
i++;
}
fclose(fp);
return0;
}
例10.4
#include
#defineSIZE10
structStudent_type
{
charname[10];
intnum;
intage;
charaddr[15];
}stud[SIZE];
voidsave()
{
FILE*fp;
inti;
if((fp=fopen("stu.dat","wb"))==NULL)
{
printf("cannotopenfile\n");
return;
}
for(i=0;iif(fwrite(&stud[i],sizeof(structStudent_type),1,fp)!
=1)
printf("filewriteerror\n");
fclose(fp);
}
intmain()
{
inti;
printf("Pleaseenterdataofstudents:
\n");
for(i=0;iscanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
save();
return0;
}
#include
#include
#defineSIZE10
structStudent_type
{
charname[10];
intnum;
intage;
charaddr[15];
}stud[SIZE];
intmain()
{
inti;
FILE*fp;
if((fp=fopen("stu.dat","rb"))==NULL)
{
printf("cannotopenfile\n");
exit(0);
}
for(i=0;i{
fread(&stud[i],sizeof(structStudent_type),1,fp);
printf("%-10s%4d%4d%-15s\n",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
}
fclose(fp);
return0;
}
从stu_list读入到stu.dat
voidload()
{
FILE*fp;
inti;
if((fp=fopen("stu_list","rb"))==NULL)
{
printf("cannotopeninfile!
\n");
return;
}
for(i=0;iif(fread(&stu[i],sizeof(structStudent_type),1,fp)!
=1)
{
if(feof(fp))
{
fclose(fp);
return;
}
printf("filereaderror\n");
}
fclose(fp);
}
intmain()
{
load();
save();
return0;
}
例10.5
#include
intmain()
{
FILE*fp1,*fp2;
fp1=fopen("file1.dat","r");
fp2=fopen("file2.dat","w");
while(!
feof(fp1))putchar(getc(fp1));
putchar(10);
rewind(fp1);
while(!
feof(fp1))putc(getc(fp1),fp2);
fclose(fp1);fclose(fp2);
return0;
}
例10.6
#include
#include
structStudent_type
{
charname[10];
intnum;
intage;
charaddr[15];
}stud[10];
intmain()
{
inti;
FILE*fp;
if((fp=fopen("stu.dat","rb"))==NULL)
{
printf("cannotopenfile\n");
exit(0);
}
for(i=0;i<10;i+=2)
{
fseek(fp,i*sizeof(structStudent_type),0);
fread(&stud[i],sizeof(structStudent_type),1,fp);
printf("%-10s%4d%4d%-15s\n",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
}
fclose(fp);
return0;
}
习题
1.stdio.h有结构体FILE的声明,由FILE定义的指向文件的指针就叫文件指针。
通过指针可以找到内存中(不是外部介质)的文件,一般一个文件一个指针。
定义指针后可以调用相关的文件函数。
2.文件的打开就是定义一个文件指针,可以找到这个文件。
文件的关闭是释放指针,不关闭文件,则可能引起数据丢失。
3.
#include
#include
intmain()
{
FILE*fp;
if((fp=fopen("test","w"))==NULL)
{
printf("can'topenthefile\n");
exit(0);
}
charch;
printf("请输入一个字符串以'!
'结束:
\n");
ch=getchar();
while(ch!
='!
')
{
if(ch>='a'&&ch<='z')ch=ch-'a'+'A';
fputc(ch,fp);
ch=getchar();
}
fclose(fp);
return0;
}
4.
#include
#include
intmain()
{
FILE*fp,*fp1,*fp2;
charch,a[200]={'\0'};
inti=0,j,k,n;
if((fp1=fopen("A","r"))==NULL)
{
printf("can'topenthefileA\n");
exit(0);
}
while(!
feof(fp1))
{
a[i]=getc(fp1);
i++;
}
fclose(fp1);
i--;
if((fp2=fopen("B","r"))==NULL)
{
printf("can'topenthefileB\n");
exit(0);
}
while(!
feof(fp2))
{
a[i]=getc(fp2);
i++;
}
fclose(fp2);
i--;
n=i;
for(i=0;i{
k=i;
for(j=i+1;jif(a[k]>a[j])k=j;
if(k!
=i)
{
ch=a[k];
a[k]=a[i];
a[i]=ch;
}
}
if((fp=fopen("C","w"))==NULL)
{
printf("can'topenthefileC\n");
exit(0);
}
for(i=0;iputc(a[i],fp);
fclose(fp);
return0;
}
5.
#include
#include
structStudent
{
longnum;
charname[20];
floatscore[3];
floataver;
}stud[5];
intmain()
{
inti;
FILE*fp;
printf("pleaseenterdataofstudent:
\n");
for(i=0;i<5;i++)
{
scanf("%ld%s%f%f%f",&stud[i].num,stud[i].name,&stud[i].score[0],&stud[i].score[1],&stud[i].score[2]);
stud[i].aver=(stud[i].score[0]+stud[i].score[1]+stud[i].score[2])/3.0;
}
for(i=0;i<5;i++)
printf("%-8ld%-15s%-5.1f%-5.1f%-5.1faver:
%-5.2f\n",stud[i].num,stud[i].name,stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].aver);
if((fp=fopen("stud","wb"))==NULL)
{
printf("cannotopenthefilestud\n");
exit(0);
}
for(i=0;i<5;i++)
if(fwrite(&stud[i],sizeof(structStudent),1,fp)!
=1)
printf("filewriteerror\n");
fclose(fp);
return0;
}
6.
#include
#include
structStudent
{
longnum;
charname[20];
floatscore[3];
floataver;
}stud[5];
intmain()
{
inti,j,k;
FILE*fp1,*fp2;
structStudenttemp;
if((fp1=fopen("stud","rb"))==NULL)
{
printf("cannotopenthefilestud\n");
exit(0);
}
for(i=0;i<5;i++)
{
if((fread(&stud[i],sizeof(structStudent),1,fp1))!
=1)
printf("filereaderror\n");
}
fclose(fp1);
for(i=0;i<4;i++)
{
k=i;
for(j=i+1;j<5;j++)
if(stud[k].aver>stud[j].aver)k=j;
if(k!
=i)
{
temp=stud[k];
stud[k]=stud[i];
stud[i]=temp;
}
}
for(i=0;i<5;i++)
printf("%-8ld%-15s%-5.1f%-5.1f%-5.1faver:
%-5.2f\n",stud[i].num,stud[i].name,stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].aver);
if((fp2=fopen("stud_sort","wb"))==NULL)
{
printf("cannotopenthefilestud\n");
exit(0);
}
for(i=0;i<5;i++)
{
if((fwrite(&stud[i],sizeof(structStudent),1,fp2))!
=1)
printf("filewriteerror\n");
}
fclose(fp2);
return0;
}
7.
#include
#include
structStudent
{
longnum;
charname[20];
floatscore[3];
floataver;
}stud[6];
intmain()
{
inti,j,k;
FILE*fp1,*fp2;
structStudenttemp;
if((fp1=fopen("stud_sort","rb"))==NULL)
{
printf("cannotopenthefilestud\n");
exit(0);
}
for(i=0;i<5;i++)
{
if((fread(&stud[i],sizeof(structStudent),1,fp1))!
=1)
printf("filereaderror\n");
}
printf("pleaseenteronestudentdata:
\n");
scanf("%ld%s%f%f%f",&stud[5].num,stud[5].name,&stud[5].score[0],&stud[5].score[1],&stud[5].score[2]);
stud[5].aver=(stud[5].score[0]+stud[5].score[1]+stud[5].score[2])/3.0;
for(i=0;i<5;i++)
{
k=i;
for(j=i+1;j<6;j++)
if(stud[k].aver>stud[j].aver)k=j;
if(k!
=i)
{
temp=stud[k];
stud[k]=stud[i];
stud[i]=temp;
}
}
for(i=0;i<6;i++)
printf("%-8ld%-15s%-5.1f%-5.1f%-5.1faver:
%-5.2f\n",stud[i].num,stud[i].name,stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].aver);
if((fp2=fopen("stud_sort1","wb"))==NULL)
{
printf("cannotopenthefilestud\n");
exit(0);
}
for(i=0;i<6;i++)
{
if((fwrite(&stud[i],sizeof(structStudent),1,fp2))!
=1)
printf("filewriteerror\n");
}
fclose(fp2);
return0;
}
8.
原文件小,所以直接覆盖了
#include
#include
structStudent
{
longnum;
charname[20];
floatscore[3];
floataver;
}stud[6];
intmain()
{
inti,j,k;
FILE*fp1,*fp2;
structStudenttemp;
if((fp1=fopen("stud_sort1","rb"))==NULL)
{
printf("cannotopenthefilestud\n");
exit(0);
}
for(i=0;i<6;i++)
{
if((fread(&stud[i],sizeof(structStudent),1,fp1))!
=1)
printf("filereaderror\n");
}
fclose(fp1);
if((fp2=fopen("stud_sort","wb"))==NULL)
{
printf("cannotopenthefilestud\n");
exit(0);
}
for(i=0;i<6;i++)
{
if((fwrite(&stud[i],sizeof(structStudent),1,fp2))!
=1)
printf("filewriteerror\n");
}
fclose(fp2);
return0;
}
9.
#include
#include
#include
structemploee
{
charname[10];
longnum;
charsex[2];
intage;
charaddr[20];
floatsalary;
charhealth[8];
chareducation[10];
}em[10];
structemp
{
charname[10];
floatsalary;
}em_case[10];
intmain()
{
FILE*fp1,*fp2;
inti,j;
if((fp1=fopen("emploee","r"))==NULL)
{
printf("cannotopenfile.\n");
exit(0);
}
for(i=0;i<10;i++)
if(fread(&em[i],sizeof(structemploee),1,fp1)!
=1)
{
printf("readerror!
\n");
exit(0);
}
fclose(fp1);
for(i=0;i<10;i++)
{
strcpy(em_case[i].name,em[i].name);
em_case[i].salary=e