C语言程序设计(第二版)第11章例题
来源:互联网 发布:绵阳师范网络管理系统 编辑:程序博客网 时间:2024/04/30 15:52
例11.1
#include<stdio.h>void main(){ FILE * fp; int i=0; char n; fp=fopen("F:\\text.txt","r"); while(!feof(fp)) { fscanf(fp,"%c",&n); printf("%c",n); i++; if(i==10){ printf("\n"); i=0; } } fclose(fp);}
例11.2
#include<stdio.h>#include<stdlib.h>void main(){ FILE * fp; int ch; if(!(fp=fopen("F:\\text.txt","w"))) { printf("Cannot open file d:\\text.txt!\n"); exit(0);}ch=getchar();while(ch!=EOF){ fputc(ch,fp); ch=getchar();}fclose(fp);return;}例11.3
#include<stdio.h>#include<stdlib.h>void main(){ FILE * fp; int ch; if(!(fp=fopen("F:\\text.txt","r"))) { printf("cannot open F:\\text.txt"); exit(0); } ch=fgetc(fp);; while(ch!=EOF) { putchar(ch); ch=fgetc(fp); } fclose(fp); return ; }例11.4
#include<stdio.h>#include<stdlib.h>static void copyFile(FILE *source,FILE * intention);static FILE * openFile(char *prompt,char * mode);void main(){ FILE * source,*intention; printf("This program copies one file to another.\n"); source=openFile("source:","r"); intention=openFile("intention:","w"); copyFile(source,intention); fclose(source); fclose(intention); return ; }static void copyFile(FILE *source,FILE * intention){ int ch; while((ch=getc(source))!=EOF) { putc(ch,intention); }}static FILE *openFile(char *prompt,char * mode){ char tmpFile[20]; FILE * reval = NULL; while(1) { printf("%s",prompt); gets(tmpFile); reval=fopen(tmpFile,mode); if(reval!=NULL) break; printf("Can't open the file \"%s\"\n",tmpFile); exit(1);}return reval;}例11.5
#include<stdio.h>#include<stdlib.h>void main(){ FILE *fp1; int i,score; if(!(fp1=fopen("f:\\text.txt","w"))){ //出错处理 printf("error"); exit(0); } for(i=0;i<10;i++) //输入成绩 { scanf("%d",&score); fprintf(fp1,"%5d",score); } fclose(fp1); return ;}例11.6
#include<stdio.h>#include<stdlib.h>#define N 100void main(){ FILE * fp; int i,n,s,a[N],x; if(!(fp=fopen("F:\\text.txt","r"))) { printf("cannot open F:\\text.txt!\n"); exit(0);}n=0;while(!feof(fp)){ fscanf(fp,"%d",&x); a[n]=x; n++;}for(s=0,i=0;i<n;i++){ s+=a[i];}printf("s=%d\n",s);fclose(fp);}
例11.7
#include<stdio.h>#include<ctype.h>#include<stdlib.h>void main(){ FILE * fp; int n1,n2,n3,n4,ch; n1=n2=n3=n4=0; if((fp=fopen("F:\\text.txt","r"))==NULL) { printf("cannot open F:\\text.txt!\n"); exit(0);}while(!feof(fp)) { ch=fgetc(fp); if(isalpha(ch)) n1++; else if(isdigit(ch)) n2++; else if(ch==' ') n3++; else n4++; } fclose(fp); printf("%d %d %d %d\n",n1,n2,n3,n4); if(fp=fopen("F:\\text.txt","a")==NULL) { printf("Error!\n"); exit(0); } fprintf(fp,"//alpha number:%d\n",n1); fprintf(fp,"//digit number:%d\n",n2); fprintf(fp,"//space number:%d\n",n3); fprintf(fp,"//other:%d\n",n4); fclose(fp);}
例11.8
#include<stdio.h>#include<stdlib.h>struct stu{ char numb[9]; int s[3]; float ave;};void main(){ FILE * f1,* f2; struct stu a,b; int flag=1; if((f1=fopen("aa.txt","r"))==NULL) { printf("Error!\n"); exit(0); } if((f2=fopen("bb.txt","w"))==NULL) { printf("Error!\n"); exit(0); } scanf("%s%d%d%d",a.numb,&a.s[0],&a.s[1],&a.s[2]); a.ave=(a.s[1]+a.s[2]+a.s[0])/3.0; while(fscanf(f1,"%8s%4d%4d%4d%6f\n",b.numb,&b.s[0],&b.s[1],&b.s[2],&b.ave) !=EOF){ if(a.ave>b.ave&&flag){ fprintf(f2,"%8s%4d%4d%4d%6.1f\n",a.numb,a.s[0],a.s[1],a.s[2],a.ave); flag = 0; } fprintf(f2,"%8s%4d%4d%4d%6.1f\n",b.numb,b.s[0],b.s[1],b.s[2],b.ave); } if(flag) fprintf(f2,"%8s%4d%4d%4d%6.1f\n",a.numb,a.s[0],a.s[1],a.s[2],a.ave); fclose(f1); fclose(f2); remove("aa.txt"); rename("bb.txt","aa.txt");}
0 0
- C语言程序设计(第二版)第11章例题
- C语言程序设计(第二版)第1章例题
- C语言程序设计(第二版)第2章例题
- C语言程序设计(第二版)第3章例题
- C语言程序设计(第二版)第4章例题
- C语言程序设计(第二版)第7章例题
- C语言程序设计(第二版)第9章例题
- 1239: 华科版C语言程序设计教程(第二版)例题6.17
- 1206: 华科版C语言程序设计教程(第二版)例题4.3
- C语言程序设计(第二版)第3章编程题
- C语言程序设计(第二版)第6章编程题
- 第7周 C语言程序设计(新2版) 例题 3个函数例题
- 谭浩强《C语言程序设计(第二版)》错误
- 第10周 C语言程序设计(新2版) 第三章例题 swich统计数字、字符和其它符号
- 简单 随机步法 C语言程序设计现代方法 第二版 第8章 编程题9
- 简单 凯撒加密 C语言程序设计现代方法 第二版 第8章 15题 列2
- 第7周 C语言程序设计(新2版) 例题1 统计输入的字符数
- 第7周 C语言程序设计(新2版) 例题 1.6数组(无法编译)
- 制作openstack SPICE高性能Windows镜像
- php获取客户端真实IP 防止代理和作弊
- hbase启动报错hbase-default.xml file seems to be for and old version of HBase
- 常见排序算法时间复杂度和空间复杂度表
- 温故而知新-----三、面向对象--静态
- C语言程序设计(第二版)第11章例题
- ubuntu安装mysql可视化工具MySQL-workbench及简单操作
- Android三种实现定时器的方法
- Android ListView隐藏FooterView(headerView)
- Mesos Persistent Storage
- Eclipse安装安卓的离线SDK包
- 2015 ACM/ICPC Asia Regional Shenyang Online-1012 Largest Point
- iOS开发—本地化
- 一招解决!!!解决ECSHOP中transport.js和jquery的冲突