博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c primer plus 习题答案(6)
阅读量:4698 次
发布时间:2019-06-09

本文共 3776 字,大约阅读时间需要 12 分钟。

p376.7

A方案

1 #include
2 #include
3 #define LEN 40 4 void rank(FILE *, FILE *); 5 int main(void) 6 { 7 FILE *fp, *fc; 8 int ch, bp; 9 char file1[LEN], file2[LEN];10 11 puts("enter file1 name");12 if((fp=fopen(gets(file1), "r"))==NULL){13 fputs("can't open", stdout);14 exit(1);15 }16 17 puts("enter file2 name");18 if((fc=fopen(gets(file2), "r"))==NULL){19 fputs("can't open", stdout);20 exit(2);21 }22 23 rank(fp, fc);24 if((fclose(fp)!=0)||(fclose(fc)!=0))25 puts("error in closing files");26 27 system("pause");28 return 0;29 }30 31 void rank(FILE *fp, FILE *fc)32 {33 int ch, bp;34 while(1){35 while(((ch=getc(fp))!='\n')&&ch!=EOF)36 putc(ch, stdout);37 if(ch=='\n')38 putc(ch, stdout);39 while(((bp=getc(fc))!='\n')&&bp!=EOF)40 putc(bp, stdout);41 if(ch=='\n')42 putc(bp, stdout);43 if((ch==EOF)&&(bp==EOF))44 break;45 }46 }

B方案

1 #include
2 #include
3 #define LEN 40 4 void rank(FILE *, FILE *); 5 int main(void) 6 { 7 FILE *fp, *fc; 8 int ch, bp; 9 char file1[LEN], file2[LEN];10 11 puts("enter file1 name");12 if((fp=fopen(gets(file1), "r"))==NULL){13 fputs("can't open", stdout);14 exit(1);15 }16 17 puts("enter file2 name");18 if((fc=fopen(gets(file2), "r"))==NULL){19 fputs("can't open", stdout);20 exit(2);21 }22 23 rank(fp, fc);24 if((fclose(fp)!=0)||(fclose(fc)!=0))25 puts("error in closing files");26 27 system("pause");28 return 0;29 }30 31 void rank(FILE *fp, FILE *fc)32 {33 int ch, bp;34 while(1){35 while(((ch=getc(fp))!='\n')&&ch!=EOF)36 putc(ch, stdout);37 while(((bp=getc(fc))!='\n')&&bp!=EOF)38 putc(bp, stdout);39 if((bp=='\n')||(ch=='\n'))40 printf("\n");41 if((ch==EOF)&&(bp==EOF))42 break;43 }44 }

p376.8

1 #include
2 #include
3 #define LEN 40 4 int main(int argc, char *argv[]) 5 { 6 FILE *fp, *fc, *fd[LEN]; 7 char name1[LEN], name2[LEN]; 8 int ch, bp, count[2]={
0}, i, num[LEN]={
0}; 9 if(argc==2){10 puts("enter file1 name");11 if((fp=fopen(gets(name1), "r"))==NULL)12 fputs("can't open", stderr);13 puts("enter file1 name");14 if((fc=fopen(gets(name2), "r"))==NULL)15 fputs("can't open", stderr);16 17 while((ch=getc(fp))!=EOF)18 if(ch==argv[1][0])19 count[0]++;20 while((bp=getc(fc))!=EOF)21 if(bp==argv[1][0])22 count[1]++;23 24 printf("%s has %d %c\n", name1, count[0], *argv[1]);25 printf("%s has %d %c\n", name2, count[1], *argv[1]);26 }27 28 for(i=2; i

p376.10

1 #include
2 #include
3 #define LEN 40 4 int main(void) 5 { 6 FILE *fp; 7 char name[LEN]; 8 long offset; 9 int ch;10 11 puts("enter the file name");12 if((fp=fopen(gets(name), "r"))==NULL){13 fprintf(stderr, "can't open\n");14 exit(1);15 }16 puts("enter an offset of file(q to quit)");17 while(scanf("%ld", &offset)==1){18 fseek(fp, offset, SEEK_SET);19 while(((ch=getc(fp))!='\n')&&ch!=EOF&&ch!='\r')20 fprintf(stdout, "%c", ch);21 if(ch==EOF)22 exit;23 printf("\n");24 puts("enter an offset of file(q to quit)");25 }26 27 fclose(fp);28 system("pause");29 return 0;30 }

转载于:https://www.cnblogs.com/coding-time/p/4526661.html

你可能感兴趣的文章
linux用户管理和文件权限
查看>>
Activiti 流程实例、任务、执行对象及相关的表
查看>>
wpa wp2 psk的配置方式
查看>>
yii性能调节
查看>>
centos 7.4 + postgresql 10.1 + pg_amqp
查看>>
Throwable、Error、Exception、RuntimeException 区别
查看>>
js对闭包的理解
查看>>
腾讯面试问题
查看>>
自定义标题栏右键菜单
查看>>
Bootstrap概述
查看>>
elementUi源码解析(1)--项目结构篇
查看>>
C#中用DateTime的ParseExact方法解析日期时间(excel中使用系统默认的日期格式)
查看>>
任务二 阅读报告
查看>>
高阶函数
查看>>
W3100SM-S 短信猫代码发送 上
查看>>
android sdk 帮助文档下载地址
查看>>
PRIVACY POLICY
查看>>
装完Win8后推荐进行的优化
查看>>
iOS 数组排序(原创)
查看>>
SQL Server中事件探测器Profiler的使用
查看>>