C Primer Plus(第五版)第13章 文件输入/输出
13.1<wbr><br>#include <stdio.h><br>#include <stdlib.h><br>int main(void)<br>{<br> int ch;<br> FILE *fp;<br> long count=0;<br> char name[100]={0};<br> puts("input the filename that you whant to operate");<br> scanf("%s",name);<br> if ((fp=fopen(name,"r"))==NULL)<br> {<br> printf("Can't open %s\n",name);<br> exit(1);<br> }<br> while ((ch=getc(fp))!=EOF)<br> {<br> putc(ch,stdout);<br> count++;<br> }<br> fclose(fp);<br> printf("File %s has %ld characters\n",name,count);<br> return 0;<br>}<br><br>13.2<br>#include<stdio.h><br>#include<stdlib.h><br>#define bufsize 1024*1024<br>int main(int num,char **sname)<br>{<br> FILE*d,*s;<br> int c;<br> char buf[bufsize];<br> char dname[100]={0};<br> sprintf(dname,"%s_copy",sname[1]);<br> if((d=fopen(dname,"wb"))==NULL)<br> {<br> fprintf(stderr,"can not to open %s\n",dname);<br> exit(EXIT_FAILURE);<br> }<br> if((s=fopen(sname[1],"rb"))==NULL)<br> {<br> fprintf(stderr,"can not to open %s\n",sname[1]);<br> exit(EXIT_FAILURE);<br> }<br> while((c=fread(buf,sizeof(char),bufsize,s))!=0)<br> fwrite(buf,sizeof(char),c,d);<br> if(ferror(s)||ferror(d))<br> {<br> fprintf(stderr,"copy failure\n");<br> exit(EXIT_FAILURE);<br> }<br> if(fclose(s)||fclose(d))<br> {<br> fprintf(stderr,"cannot close flie\n");<br> exit(EXIT_FAILURE);<br> }<br> return 0;<br>}<br><br>13.3<br>#include<stdio.h><br>#include<stdlib.h><br>#include<ctype.h><br>#define bufsize 1024*1024<br>int main(void)<br>{<br> char s[100],d[100];<br> FILE*sp,*dp;<br> int c;<br> puts("input the source filename for copy");<br> scanf("%s",s);<br> puts("input the destination filename");<br> scanf("%s",d);<br> if((sp=fopen(s,"r"))==NULL)<br> {<br> fprintf(stderr,"can not open the %s file",s);<br> exit(EXIT_FAILURE);<br> }<br> if((dp=fopen(d,"w"))==NULL)<br> {<br> fprintf(stderr,"can not open the %s file",d);<br> exit(EXIT_FAILURE);<br> }<br> while((c=getc(sp))!=EOF)<br> putc(toupper(c),dp);<br> if(ferror(sp)||ferror(dp))<br> {<br> fprintf(stderr,"copy failure\n");<br> exit(EXIT_FAILURE);<br> }<br> if(fclose(sp)||fclose(dp))<br> {<br> fprintf(stderr,"cannot close flie\n");<br> exit(EXIT_FAILURE);<br> }<br> return 0;<br>}<br><br>13.4<br>#include<stdio.h><br>#include<stdlib.h><br>#define bufsize 1024*1024<br>int main(int num,char**f)<br>{<br> int i,c;<br> FILE*s;<br> char buf[bufsize]={0};<br> for(i=1;i<num;i++)<br> {<br> if((s=fopen(f[i],"rb"))==NULL)<br> {<br> fprintf(stderr,"%s can not open.\n",f[i]);<br> exit(EXIT_FAILURE);<br> }<br> while((c=fread(buf,sizeof(char),bufsize,s))!=0)<br> fwrite(buf,sizeof(char),c,stdout);<br> if(ferror(s)||ferror(stderr))<br> {<br> fprintf(stderr,"a problem detectied\n");<br> exit(EXIT_FAILURE);<br> }<br> if(fclose(s))<br> {<br> fprintf(stderr,"a problem detectied\n");<br> exit(EXIT_FAILURE);<br> }<br> }<br> return 0;<br>}<br><br>13.5<br>#include <stdio.h><br>#include <stdlib.h><br>#include <string.h><br>#define BUFSIZE 1024<br>void append(FILE *source, FILE *dest);<br>int main(int num,char**p)<br>{<br> FILE *fa,*fs;<br> int files=0;<br> int i=1;<br> if ((fa=fopen(p[1],"ab"))==NULL)<br> {<br> fprintf(stderr,"Can't open %s\n",p[1]);<br> exit(2); <br> }<br> if (setvbuf(fa,NULL,_IOFBF,BUFSIZE)!=0)<br> {<br> fputs("Can't create output buffer\n",stderr);<br> exit(3);<br> }<br> while (++i<num)<br> {<br> if (strcmp(p[1],p[i])==0)<br> fputs("Can't append file to itself\n",stderr);<br> else if ((fs=fopen(p[i],"rb"))==NULL)<br> fprintf(stderr,"Can't open %s\n",p[i]);<br> else<br> {<br> if (setvbuf(fs,NULL,_IOFBF,BUFSIZE)!=0)<br> {<br> fputs("Can't create input buffer\n",stderr);<br> continue;<br> }<br> append(fs, fa);<br> if (ferror(fs)!=0)<br> fprintf(stderr,"Error in reading file %s.\n",p[i]);<br> if (ferror(fa)!=0)<br> fprintf(stderr,"Error in writing file %s.\n",p[1]);<br> fclose(fs);<br> files++;<br> printf("File %s appended.\n",p[i]);<br> }<br> }<br> printf("Done. %d files appended.\n", files);<br> fclose(fa);<br> return 0;<br>}<br>void append(FILE *source, FILE *dest)<br>{<br> size_t bytes;<br> static char temp[BUFSIZE];<br> while ((bytes=fread(temp,sizeof(char),BUFSIZE,source))>0)<br> fwrite(temp,sizeof(char),bytes,dest);<br>}<br><br>13.6<br>#include <stdio.h><br>#include <stdlib.h><br>#include <string.h><br>#define LEN 40<br>int main(void)<br>{<br> FILE *in,*out;<br> int ch,i;<br> char c;<br> char name[LEN];<br> char iname[LEN];<br> puts("type the input filename");<br> for(i=0;(c=getchar())!='\n'&&i<LEN-1;i++)<br> iname[i]=c;<br> iname[i]=0;<br> int count=0;<br> if ((in=fopen(iname,"r"))==NULL)<br> {<br> fprintf(stderr,"I couldn't open the file \"%s\"\n",iname);<br> exit(2);<br> }<br> strncpy(name,iname,LEN-5);<br> name[LEN-5]='\0';<br> strcat(name,".red");<br> if ((out=fopen(name,"w"))==NULL)<br> {<br> fprintf(stderr,"Can't create output file.\n");<br> exit(3);<br> }<br> while ((ch=getc(in))!=EOF)<br> if (count++%3==0)<br> putc(ch,out);<br> if (fclose(in)!=0||fclose(out)!=0)<br> fprintf(stderr,"Error in closing files\n");<br> return 0;<br>}<br><br>13.7.a<br>#include<stdio.h><br>#include<stdlib.h><br>#define len 81<br>int main(void)<br>{<br> FILE *f1,*f2;<br> int c1,c2;<br> char n1[len],n2[len];<br> puts("input the first filename");<br> while(fscanf(stdin,"%s",n1)==0)<br> puts("wrong,do it again");<br> puts("input the second filename");<br> while(fscanf(stdin,"%s",n2)==0)<br> puts("wrong,do it again");<br> if((f1=fopen(n1,"r"))==NULL)<br> {<br> fprintf(stderr,"can not open %s\n",n1);<br> exit(EXIT_FAILURE);<br> }<br> if((f2=fopen(n2,"r"))==NULL)<br> {<br> fprintf(stderr,"can not open %s\n",n2);<br> exit(EXIT_FAILURE);<br> }<br> c1=getc(f1);<br> c2=getc(f2);<br> while(c1!=EOF||c2!=EOF)<br> {<br> while(c1!=EOF&&c1!='\n')<br> {<br> putchar(c1);<br> c1=getc(f1);<br> }<br> if(c1=='\n') putchar(c1);<br> while(c2!=EOF&&c2!='\n')<br> {<br> putchar(c2);<br> c2=getc(f2);<br> }<br> if(c2=='\n') putchar(c2);<br> c1=getc(f1);<br> c2=getc(f2);<br> }<br> if(fclose(f1)||fclose(f2))<br> {<br> fprintf(stderr,"cannot close flie\n");<br> exit(EXIT_FAILURE);<br> }<br> return 0;<br>}<br><br>13.7.b<br>#include<stdio.h><br>#include<stdlib.h><br>#define len 81<br>int main(void)<br>{<br> FILE *f1,*f2;<br> int c1,c2;<br> char n1[len],n2[len];<br> puts("input the first filename");<br> while(fscanf(stdin,"%s",n1)==0)<br> puts("wrong,do it again");<br> puts("input the second filename");<br> while(fscanf(stdin,"%s",n2)==0)<br> puts("wrong,do it again");<br> if((f1=fopen(n1,"r"))==NULL)<br> {<br> fprintf(stderr,"can not open %s\n",n1);<br> exit(EXIT_FAILURE);<br> }<br> if((f2=fopen(n2,"r"))==NULL)<br> {<br> fprintf(stderr,"can not open %s\n",n2);<br> exit(EXIT_FAILURE);<br> }<br> c1=getc(f1);<br> c2=getc(f2);<br> while(c1!=EOF||c2!=EOF)<br> {<br> while(c1!=EOF&&c1!='\n')<br> {<br> putchar(c1);<br> c1=getc(f1);<br> }<br> while(c2!=EOF&&c2!='\n')<br> {<br> putchar(c2);<br> c2=getc(f2);<br> }<br> puts("");<br> c1=getc(f1);<br> c2=getc(f2);<br> }<br> if(fclose(f1)||fclose(f2))<br> {<br> fprintf(stderr,"cannot close flie\n");<br> exit(EXIT_FAILURE);<br> }<br> return 0;<br>}<br><br>13.8<br>#include<stdio.h><br>#include<stdlib.h><br>int main(int num,char **p)<br>{<br> int c,i,k;<br> i=0;<br> FILE*f;<br> if(num==1)<br> {<br> puts("at least one argument.");<br> exit(EXIT_FAILURE);<br> }<br> else if(num==2)<br> {<br> while((c=getc(stdin))!=EOF)<br> if(c==p[1][0])<br> i++;<br> fprintf(stdout,"in stdin the %c appear %d times\n",p[1][0],i);<br> }<br> else<br> for(k=0;k<num-2;k++)<br> {<br> i=0;<br> if((f=fopen(p[k+2],"r"))==NULL)<br> fprintf(stderr,"can not open the %s \n",p[k+2]);<br> else<br> {<br> while((c=getc(f))!=EOF)<br> if(c==p[1][0])<br> i++;<br> fprintf(stdout,"in %s the %c appear %d times\n",p[k+2],p[1][0],i);<br> if(fclose(f)!=0)<br> fprintf(stderr,"Error closing file\n");<br> }<br> }<br> return 0;<br>}<br><br>13.9<br>#include <stdio.h><br>#include <stdlib.h><br>#include<string.h><br>#define MAX 40<br>int main(void)<br>{<br> FILE*fp;<br> char i=0;<br> char words[MAX];<br> char temp[MAX+5]={0};<br> if ((fp=fopen("wordy","a+"))==NULL)<br> {<br> fprintf(stdout,"Can't open \"words\" file.\n");<br> exit(1);<br> }<br> puts("Enter words to add to the file; press the Enter");<br> puts("key at the beginning of a line to terminate.");<br> while (fgets(words,MAX,stdin)!=NULL&&words[0]!='\n')<br> {<br> i++;<br> sprintf(temp,"%d",i);<br> strcat(temp,words);<br> fprintf(fp,"%s",temp);}<br> puts("File contents:");<br> rewind(fp);<br> while (fscanf(fp,"%s",words)==1)<br> puts(words);<br> if (fclose(fp)!=0)<br> fprintf(stderr,"Error closing file\n");<br> return 0;<br>}<br><br>13.10<br>#include<stdio.h><br>#include<stdlib.h><br>#define size 81<br>int main(void)<br>{<br> char name[size];<br> FILE*f;<br> char c;<br> puts("input a filename for operate EOF to quit");<br> while(fscanf(stdin,"%s",name)==1)<br> {<br> while(getchar()!='\n')<br> ;<br> if((f=fopen(name,"r"))==NULL)<br> {<br> puts("can not open this file");<br> exit(EXIT_FAILURE);<br> }<br> while((c=getc(f))!='\n')<br> putc(c,stdout);<br> puts("");<br> if(fclose(f)!=0)<br> {<br> puts("can not close this file");<br> exit(EXIT_FAILURE);<br> }<br> puts("input a filename for operate EOF to quit");<br> }<br> return 0;<br>}<br><br>13.11<br>#include<stdio.h><br>#include<stdlib.h><br>#include<string.h><br>#define size 1024*1024<br>int main(int num,char**p)<br>{<br> FILE*f;<br> char s[size];<br> if(num!=3)<br> {<br> fprintf(stderr,"wrong input,use %s argument1 argument2\n",p[0]);<br> exit(EXIT_FAILURE);<br> }<br> if((f=fopen(p[2],"r"))==NULL)<br> {<br> fprintf(stderr,"can not open the %s",p[2]);<br> exit(EXIT_FAILURE);<br> }<br> while(fgets(s,size,f)!=NULL)<br> if(strstr(s,p[1])!=NULL)<br> fputs(s,stdout);<br> if(fclose(f)!=0)<br> {<br> fprintf(stderr,"can not close the %s",p[2]);<br> exit(EXIT_FAILURE);<br> }<br> return 0;<br>}<br><br>13.12<br>#include<stdio.h><br>#include<stdlib.h><br>#include<string.h><br>#define size 81<br>#define row 20<br>#define clu 31<br>char change(char);<br>int main(void)<br>{<br> FILE*s,*d;<br> int i,j;<br> int *c,ch;<br> char name[size];<br> int matrix[row][clu]={0};<br> puts("input the graphic filename");<br> fscanf(stdin,"%s",name);<br> if((s=fopen(name,"r"))==NULL)<br> {<br> puts("cannot open the file");<br> exit(EXIT_FAILURE);<br> }<br> strcat(name,"_graphic");<br> if((d=fopen(name,"w"))==NULL)<br> {<br> fprintf(stderr,"cannot open the %s",name);<br> exit(EXIT_FAILURE);<br> }<br> for(i=0;i<row;i++)<br> for(j=0;j<clu;j++)<br> {<br> while((ch=getc(s))==' ')<br> ;<br> matrix[i][j]=ch;<br> }<br> c=(int*)matrix;<br> for(i=0;i<row*clu;i++)<br> putc(change(c[i]),d);<br> if(fclose(s)||fclose(d))<br> {<br> puts("cannot close the file");<br> exit(EXIT_FAILURE);<br> }<br> return 0;<br>}<br>char change(char c)<br>{<br> if(!isdigit(c))<br> return c;<br> switch(c)<br> {<br> case '0':<br> return ' ';<br> case '1':<br> return '.';<br> case '2':<br> return '\'';<br> case '3':<br> return ':';<br> case '4':<br> return '~';<br> case '5':<br> return '*';<br> case '6':<br> return '=';<br> case '7':<br> return c;<br> case '8':<br> return '%';<br> case '9':<br> return '#';<br> }<br>}<br><br>13.13<br>#include<stdio.h><br>#include<stdlib.h><br>#include<string.h><br>#define size 81<br>#define row 20<br>#define clu 30<br>char change(char);<br>int main(void)<br>{<br> FILE*s,*d;<br> int i,j;<br> int ch;<br> char name[size];<br> int matrix[row+2][clu+2]={0};<br> puts("input the graphic filename");<br> fscanf(stdin,"%s",name);<br> if((s=fopen(name,"r"))==NULL)<br> {<br> puts("cannot open the file");<br> exit(EXIT_FAILURE);<br> }<br> strcat(name,"_graphic");<br> if((d=fopen(name,"w"))==NULL)<br> {<br> fprintf(stderr,"cannot open the %s",name);<br> exit(EXIT_FAILURE);<br> }<br> for(i=1;i<row+1;i++)<br> for(j=1;j<clu+1;j++)<br> {<br> ch=getc(s);<br> while(ch==' '||ch=='\n')<br> ch=getc(s);<br> matrix[i][j]=ch;<br> }<br> _Bool up,down,left,right;<br> int c;<br> for(i=1;i<row+1;i++)<br> for(j=1;j<clu+1;j++)<br> {<br> up=down=left=right=0;<br> c=4;<br> if(j-1>=1){if(matrix[i][j]-matrix[i][j-1]>1)left=1;}<br> else{left=1;c--;}<br> if(j+1<clu+1){if(matrix[i][j]-matrix[i][j+1]>1)right=1;}<br> else{right=1;c--;}<br> if(i-1>=1){if(matrix[i][j]-matrix[i-1][j]>1)up=1;}<br> else{up=1;c--;}<br> if(i+1<row+1){if(matrix[i][j]-matrix[i+1][j]>1)down=1;}<br> else{down=1;c--;}<br> if(left&&right&&up&&down)<br> matrix[i][j]=(matrix[i-1][j]+matrix[i+1][j]+matrix[i][j-1]+matrix[i][j+1])/c;<br> }<br> for(i=1;i<row+1;i++)<br> {<br> for(j=1;j<clu+1;j++)<br> putc(matrix[i][j],d);<br> putc('\n',d);<br> }<br> if(fclose(s)||fclose(d))<br> {<br> puts("cannot close the file");<br> exit(EXIT_FAILURE);<br> }<br> return 0;<br>}</wbr>
相关推荐
C Primer Plus 第五版第九章答案,自己编写可以成功运行
( C Primer Plus(第五版)中文版课后编程题全解,非常全,亲测可用
C Primer Plus(第五版)中文版.txt fhnfdjjy
c primer plus 第五版课后习题答案
c++ primer plus 第五版课后习题答案
在这里下载了C Primer Plus(第五版)中文版可惜没有书签,就用OCR获取了书签生成了用 PdgCntEditor 1.07a 可以使用的格式共大家自行添加书签。不过原书是加密的不能编辑还有借助解密工具PDF Password Remover v3.0 。...
C Primer Plus第6版编程练习答案.pdf
C++PrimerPlus第五版习题参考答案
C Primer Plus第五版中文版是全球最经典的C语言教程,(包括PDF,书中源码,习题解答)。由于资格的原因,分4个RAR上传,都要下载。
C Primer Plus(第五版)中文版,学习C语言必备,讲的特别详细
c入门必看的c primer plus ,其课后练习题也是很不错的。
《C Primer Plus》第六版 第十一章编程练习答案,亲测编译通过
c primer plus 第六版 中文版 源代码+勘误+习题答案,从官方处获得,因为官网需注册很麻烦,所以索性上传,与大家分享。 共三部分,针对第六版,含课后习题,源代码,书中勘误。
c++ primer plus 课后编程练习的完整答案 PDF文件.
通过学习《C Primer Plus(第五版)中文版》,你将奠定坚实的C编程基础。 与以前的版本一样,作者的目标仍旧是为读者提供一本入门性、条理清晰、见解深刻的C语言教程。作者把编程概念和C语言的细节很好地融合在一起...
C++Primer Plus(第6版)中文版编程练习答案--第四章.pdf
本书是c++ primer plus第五版的源代码,以及好多高手的编程心路沥程,不得不看!! 听君一席话胜读十年书!
《C++ Primer Plus第6版中文版》学习笔记(第十章) 红字内容是有疑问或者没把握的。 绿字部分是比较重要,或者经过确认的
C Primer Plus (第五版) 课后编程练习答案 (完整) 文字版
c primer plus 第五版课后编程的全部答案