8 1 2 ASCII 00110001 00110000 1 0

  • Slides: 57
Download presentation

8. 1. 2 文件的类别 文本文件(ASCII文件) 00110001 00110000 (1) (0) (0) 二进制文件 00000000 00100111 00010000

8. 1. 2 文件的类别 文本文件(ASCII文件) 00110001 00110000 (1) (0) (0) 二进制文件 00000000 00100111 00010000 (10000) 8

#include <stdio. h> 【例8. 1】打开名为“a. txt”的 文件,并向文件输出字符 #include <stdlib. h> 串“Test. File”,然后关闭 void main()

#include <stdio. h> 【例8. 1】打开名为“a. txt”的 文件,并向文件输出字符 #include <stdlib. h> 串“Test. File”,然后关闭 void main() 用exit函数时加 文件,同时在屏幕上输出 { fclose的返回值。 FILE *fp. File; int n. Status=0; If(fp. File=fopen("a. txt", "w+"))==NULL) { 读写方式 printf(“Open file failed!n”); exit(0); 中断正在执行的程序 } fprintf(fp. File, "%s", "Test. File"); n. Status=fclose(fp. File); printf("%d", n. Status); }

#include <stdio. h> 【例8. 2】从键盘读取字符,并 #include <stdlib. h> 输出到“text. txt”文件中。 void main() 用exit函数时加 {

#include <stdio. h> 【例8. 2】从键盘读取字符,并 #include <stdlib. h> 输出到“text. txt”文件中。 void main() 用exit函数时加 { FILE *fp. File; 只写 char c; if(fp. File=fopen("c: \test. txt", "w"))==NULL) { printf("Open file failed!n"); exit(0); } while((c=getchar())!='Q') 不断输入字符,直到 输入字母’Q’为止。 fputc(c, fp. File); fclose(fp. File); }

#include <stdio. h> 【例8. 3】有10个学生考了C语言 #include <stdlib. h> 这门课,编程将学生成绩输入 void main() 到文件c: datascore. txt中。

#include <stdio. h> 【例8. 3】有10个学生考了C语言 #include <stdlib. h> 这门课,编程将学生成绩输入 void main() 到文件c: datascore. txt中。 { FILE *fp; int i, grade; if(fp=fopen(“c: \data\score. txt”, ”w”))==NULL) {fprintf(stderr, “Error opening file c: \data\score. txtn”); 只写 exit(1); 标准出错信息文件(对应于终 端屏幕)。 } printf(“input 10 score: n”); for(i=1; i<=10; i++) { scanf(“%d”, &grade); fprintf(fp, ”%5 d”, grade); 数据输出到文件 if(i==5) fprintf(fp, “n”); } fclose(fp); }

#include <stdio. h> #include <stdlib. h>【例8. 4】将例8 -3中创建的文件 void main() c: datascore. txt中数据读出 {

#include <stdio. h> #include <stdlib. h>【例8. 4】将例8 -3中创建的文件 void main() c: datascore. txt中数据读出 { FILE *fp; 来并显示。 int i=0, grade; 只读 if(fp=fopen(“c: \data\score. txt”, ”r”))==NULL) { fprintf(stderr, “Error opening file c: \data\score. txtn”); exit(1); feof()判断文件指针是否指向 } 文件结束位置,如果指向文件 printf(“scores : n”); 结束位置,则该函数返回非 0数 while(!feof(fp)) 据;否则返回 0。 { fscanf(fp, “%d”, &grade); printf(”%5 d”, grade); i++; if(i==5) printf(“n”); } printf(“n. There are %d numbern”, i); fclose(fp);

#include <stdio. h> #include <stdlib. h> 【例8. 5】输入 5个学生数据到文 struct stud 件d: stud. dat中,然后从文

#include <stdio. h> #include <stdlib. h> 【例8. 5】输入 5个学生数据到文 struct stud 件d: stud. dat中,然后从文 { char name[20]; 件中把数据读出来并显示。 int age; char num[20]; }s[5], t; void main() { FILE *fp; 只写(二进制文件) int i=0; if((fp=fopen("d: \stud. dat", "wb"))==NULL) {printf("Error opening file d: \stud. datn"); exit(1); } 输入 5个学生数据 while(i<5) { printf("input name: "); scanf("%s", s[i]. name); printf("input age: "); scanf("%d", &s[i]. age); printf("input number: "); scanf("%s", &s[i]. num); i++; }

if(fwrite(s, sizeof(struct stud), 5, fp)!=5) { printf("Error writing file d: \stud. datn"); exit(1); }

if(fwrite(s, sizeof(struct stud), 5, fp)!=5) { printf("Error writing file d: \stud. datn"); exit(1); } 将5个学生数据写入二进制文件 fclose(fp); if((fp=fopen("d: \stud. dat", "rb"))==NULL) { printf("Error opening file d: \stud. datn"); exit(1); } 只读(二进制文件) i=0; while(!feof(fp)) { if(fread(&t, sizeof(struct stud), 1, fp)!=1) { printf("Error reading file d: \stud. datn"); exit(1); } i++; printf("the %dth student: ", i); printf(" name: %s ", t. name); printf("age: %d ", t. age); printf("number: %sn", t. num); } fclose(fp); 从二进制文件读出 5个学生数据 }

#include <stdio. h> 【例8. 6】编程读出文件 #include <stdlib. h> stud. dat中第三个学生 struct student 的数据。 {

#include <stdio. h> 【例8. 6】编程读出文件 #include <stdlib. h> stud. dat中第三个学生 struct student 的数据。 { char name[20]; int age; char num[20]; }; void main() { struct student stud; FILE *fp; 只读(二进制文件) int i=2; if(fp=fopen(“stud. dat”, ”rb”))==NULL) { printf(“Error opening file! n”); exit(1); }

使文件指针相对文件头偏移i个学生数 据量,即指向第i+1个学生数据。 fseek(fp, i*sizeof(struct student), SEEK_SET); if (fread(&stud, sizeof(structstudent), 1, fp)==1) { printf(“%s, %d,

使文件指针相对文件头偏移i个学生数 据量,即指向第i+1个学生数据。 fseek(fp, i*sizeof(struct student), SEEK_SET); if (fread(&stud, sizeof(structstudent), 1, fp)==1) { printf(“%s, %d, %sn”, stud. name, 从文件中读出一块结构体类 stud. age, stud. num); } 型数据放在stud变量中 else printf(“record 3 does not presented. n”); fclose(fp); }

【例8. 7】模拟DOS系统的 #include <stdio. h> COPY命令,实现文件复 #include <stdlib. h> 制。 void main() { FILE

【例8. 7】模拟DOS系统的 #include <stdio. h> COPY命令,实现文件复 #include <stdlib. h> 制。 void main() { FILE *fp. From, *fp. To; char sz. From. File[20]; char sz. To. File[20]; int c; printf("Please input the from file name: n"); scanf("%s", sz. From. File); printf("Please input the to file name: n"); scanf("%s", sz. To. File);

if(fp. From=fopen(sz. From. File, "r"))==NULL) { printf("Open file failed!n"); exit(0); } if((fp. To=fopen(sz. To.

if(fp. From=fopen(sz. From. File, "r"))==NULL) { printf("Open file failed!n"); exit(0); } if((fp. To=fopen(sz. To. File, "w"))==NULL) { printf("Open file failed!n"); exit(0); } while((c=fgetc(fp. From))!=EOF) fputc(c, fp. To); fclose(fp. From); fclose(fp. To); }

if (fp 2=fopen(“data 1. dat”, ”w”))==NULL) { printf(“不能建立文件n”); return; } while (!feof(fp 1)) {

if (fp 2=fopen(“data 1. dat”, ”w”))==NULL) { printf(“不能建立文件n”); return; } while (!feof(fp 1)) { c=fgetc(fp 1); c=(c+1)%256; fputc(c, fp 2); } fclose(fp 1); fclose(fp 2); }