C Prime Plus第十三章
2015-09-21 20:02
330 查看
1.
#include <stdio.h> #include <stdlib.h> int main(void) { char name[41]; int ch; FILE *fp; long count = 0; printf("Input the file's name: "); scanf("%s", &name); if((fp = fopen(name, "r")) == NULL) { printf("Can't open %s\n", name); exit(1); } while((ch = getc(fp)) != EOF) { putc(ch, stdout); count++; } fclose(fp); printf("File %s has %ld characters\n", name, count); return 0; }
2.
#include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { int ch; FILE *source, *desitination; if((source = fopen(argv[1], "rb")) == NULL) { fprintf(stderr, "Can't open.\n"); exit(1); } if((desitination = fopen(argv[2], "rb")) == NULL) { fprintf(stderr, "Can't open.\n"); exit(2); } while((ch = getc(source)) != EOF) putc(ch, desitination); fclose(source); fclose(desitination); fprintf(stdout, "copy finished.\n"); return 0; }
3.
#include <stdio.h> #include <stdlib.h> #include <ctype.h> int main(int argc, char *argv[]) { int ch; FILE *source, *desitination; char s_name[41], d_name[41]; printf("Input the source name:"); if(source = fopen(gets(s_name), "r")) { printf("Can't open.\n"); exit(1); } printf("Input the desitination:"); if(source = fopen(gets(d_name), "r")) { printf("Can't open.\n"); exit(1); } while((ch = getc(source)) != 0) putc(toupper(ch), desitination); fclose(source); fclose(desitination); pritnf("copy finished.\n"); return 0; }
5.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFSIZE 1024 #define SLEN 81 void append(FILE * source, FILE *dest); int main(int argc, int argv[]) { FILE *fa, *fs; int files = 0, i; char file_app[SLEN] ; char file_src[SLEN]; if((fa = fopen(argv[1],"a")) == NULL) { fprintf(stderr, "Can't open %s\n", file_app); exit(2); } if(setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0) { fputs("Can't create output buffer\n", stderr); exit(3); } for(i = 2; i<argc; i++) { if(strcmp(argv[i], argv[1]) == 0) fputs("Can't append file to itself\n", stderr); else if((fs = fopen(argv[i], "r")) == NULL) fprintf(stderr, "Can't open %s\n", argv[1]); else { if(setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0) { fputs("Can't create input buffer.\n", stderr); continue; } append(fs, fa); if(ferror(fs) != 0) fprintf(stderr, "Error in reading file %s.\n", argv[1]); if(ferror(fa) != 0) fprintf(stderr, "Error in writing file %s.\n", argv[i]); fclose(fs); files++; printf("File %s append.\n", argv[1]); puts("Next file(empty line to quit):"); } } printf("Done. %d files append.\n", files); fclose(fa); return 0; } void append(FILE *source, FILE *dest) { size_t bytes; static char temp[BUFSIZE]; while((bytes = fread(temp, sizeof(char), BUFSIZE, source)) > 0) fwrite(temp, sizeof(char), bytes, dest); }
6.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define LEN 40 int main(void) { FILE *in, *out; int ch; char name[LEN]; int count = 0; puts("Enter the file's name to reduce:"); gets(name); if((in = fopen(name,"r")) == NULL) { fprintf(stderr, "I couldn't open the file \%s\n", name); exit(2); } name[LEN - 5] = '\0'; strcat(name, ".red"); if((out = fopen(name, "w")) == NULL) { fprintf(stderr, "Can't create output file.\n"); exit(3); } while((ch = getc(in)) != EOF) if(count++ %3 == 0) putc(ch, out); if(fclose(in) != 0 || fclose(out) != 0) fprintf(stderr, "Error in closing files\n"); return 0; }
7.
a.#include
8.
#include <stdio.h> #include <stdlib.h> int count(char ch, FILE *fp); int main(int argc, int argv[]) { FILE *fp; char ch; int i; if(argv < 2) { printf("No char?\n"); exit(1); } ch = argv[1][0]; if(argv == 2) { printf("Input your article:"); printf("In your input: %c appeared %d times.\n", ch, count(ch, stdin)); } else for(i=2; i<argc; i++) { if((fp = fopen(argv[i], "r")) == NULL) printf("Can't open %s.\n", argv[i]); else { printf("In %s:%c has appeared %d times.\n", argv[i], ch, count(ch, fp)); fclose(fp); } } return 0; } int count(char ch, FILE *fp) { int n=0; char ch2; while((ch2 = getc(fp)) != EOF) if(ch == ch2) n++; return 0; }
9.
#include <stdio.h> #include <stdlib.h> #define MAX 40 int main(void) { FILE *fp; char words[MAX]; int count = 0; if((fp = fopen("word", "a+")) == NULL) { fprintf(stdout, "Can't open \"words\" file.\n"); exit(1); } rewind(fp); while(fgets(words, MAX-1, fp) != NULL) count++; puts("Enter words to add to the file;press the Enter"); puts("key at the beginning of a line to terminate."); while(gets(words) != NULL && words[0] != '\0') fprintf(fp, "%s", words); puts("File contents:"); rewind(fp); while(fscanf(fp, "%s", words) == 1) puts(words); if(fclose(fp) != 0) fprintf(stderr, "Error closing file\n"); return 0; }
10.
#include <stdio.h> #include <stdlib.h> #define MAX 81 int main(void) { char name[30],content[MAX]; int row, column; FILE *fp; printf("input the name of file:"); gets(name); if((fp =fopen(name, "r")) == NULL) { printf("Can't open %s", name); exit(1); } printf("input the row and columnto output:"); while(scanf("%d%d", &row, &column) == 2) { row--, column--; fseek(fp, 0, SEEK_SET); while(row--) fgets(content, MAX, fp); printf(content); printf("input the start position to output:"); } printf("Quit.\n"); return 0; }
11.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 201 int main(int argc, int argv[]) { FILE *fp; char str[MAX]; if((fp = fopen(argv[2], "r")) == NULL) { printf("Can't open %s.\n", fp); exit(1); } while(fgets(str, MAX, fp) != NULL) if(strstr(str, argv[1]) != NULL) printf(str); return 0; }
相关文章推荐
- Lua和C语言的交互详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法
- C语言实现顺序表基本操作汇总
- C语言中计算正弦的相关函数总结
- 使用C语言详解霍夫曼树数据结构
- 探讨C语言的那些小秘密之断言
- C语言实现BMP转换JPG的方法
- 深入探讨C语言中局部变量与全局变量在内存中的存放位置
- C语言查找数组里数字重复次数的方法
- C语言泛型编程实例教程
- C语言中使用lex统计文本文件字符数
- 在C语言中转换时间的基本方法介绍
- C语言进制转换代码分享