html5-img
1 / 40

第 10 章 文 件

第 10 章 文 件. 10.1 文件的概述 10.2 文件的基本操作. 例题链接. 10.1 文件的概述. 文件的概念 文件 —— 存储在外部介质(如磁盘)上的数据的集合 操作系统是以文件为单位对数据进行管理的 . 按介质划分:磁盘文件、磁带文件 按文件内容划分:源程序文件、数据文件 按组织形式划分:文本文件和二进制文件 输入输出是数据传送的过程,数据如流水一样从一处流向另一处,因此常将输入输出形象地称为流 (stream) ,即输入输出流。

zasha
Télécharger la présentation

第 10 章 文 件

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第10章 文 件 10.1 文件的概述 10.2 文件的基本操作 例题链接

  2. 10.1文件的概述 文件的概念 文件 —— 存储在外部介质(如磁盘)上的数据的集合 操作系统是以文件为单位对数据进行管理的. 按介质划分:磁盘文件、磁带文件 按文件内容划分:源程序文件、数据文件 按组织形式划分:文本文件和二进制文件 输入输出是数据传送的过程,数据如流水一样从一处流向另一处,因此常将输入输出形象地称为流(stream),即输入输出流。 C语言把文件看作是一个字符(字节)的序列,即由一个一个字符(字节)的数据顺序组成。一个输入输出流就是一个字节流或二进制流。 返回 P290

  3. 00000001 10110000 • 文本文件和二进制文件 占2个字节(TC) 432 按二进制文件存储 不能用记事本显示 但速度快 4 3 2 占3个字节 00000100 00000011 00000010 按文本文件存储 能用记事本显示 但速度慢

  4. . 4 1 2 3 • 文本文件和二进制文件 占4个字节 1.234 按二进制文件存储 占5个字节 按文本文件存储

  5. 数据文件 特点:文件中存放的都是数据,这些数据可以长期保留,可以随时存取

  6. 返回 10.2 文件的基本操作 P291 • “写”文件: 将数据从内存输出到磁 盘文件 • “读”文件: 从已建立的数据文件中将所要的数据输入到内存

  7. 编程点拨 向文件输出字符串的操作与用printf进行“输出”十分相似 对文件进行操作时,需要先打开文件,操作完毕还要关闭文件 【例10.1】向文件输出数据的示例 将“Let’s study the C language.”输出到一个文本文件

  8. #include <stdio.h> main( ) { char a[80]= "Let’s study the C language."; FILE *fp=NULL; fp=fopen("a.txt","w"); fprintf(fp, "%s", a); fclose(fp); } 定义文件指针fp 用“写”方式打开文本文件a.txt 将数组a中的字符串输出到fp所指的文件中 关闭文件 屏幕上无任何显示

  9. 编程点拨 从文件读取数据的操作与用scanf 进行“输入”十分相似 也需要先打开文件,操作完毕后关闭文件 【例10.2】从文件读取数据的示例 将例10.1所建文件“a.txt”中的内容 读取出来,并输出到屏幕上

  10. #include <stdio.h> main( ) { char a[80]=""; FILE *fp=NULL; fp=fopen("a.txt","r"); fscanf(fp, "%s", a); puts(a); fclose(fp); } 为“读取”打开文本文件a.txt 从fp所指的文件中读入字符串,并存放在数组a中 Let’s 注意:不输出Let’s study the C language.

  11. 文件操作的说明: • 定义文件指针 FILE *文件指针名; 文件指针是一个名为FILE(必须大写)的结构体类型的指针 需要加#include <stdio.h>

  12. 文件操作的说明: 单击此处查看 单击可查看 • 打开文件 fopen(文件名,打开方式) 字符串 fp = fopen("a.txt","r"); if ( fp == NULL ) { printf("Can’t open this file"); exit(0); }

  13. 标准输入输出函数 文件的读写操作是通过调用标准输入输出函数完成 • 关闭文件 fclose(文件指针)

  14. 【例10.3】输入若干学生的成绩(整型) , 用-1结束,调用fprintf函数,按格式将学生的成绩写入d:\cwz\b.txt中

  15. #include <stdio.h> main( ) { FILE *fp=NULL; int a=0; fp=fopen("d:\\cwz\\b.txt","w"); if ( fp == NULL ) { printf("Can't open file !\n"); exit(0); } scanf("%d", &a); while( a != -1 ) { fprintf( fp,"%4d", a ); scanf( "%d", &a ); } fclose(fp); }

  16. 60 70 90 100 80 60 –1 屏幕上无任何信息,但在d盘cwz目录下可以找到b.txt文件,且文件中的内容是:60 70 90 100 80 60 如果在d盘下没有cwz目录,则文件打开失败,这时屏幕上显示信息:“Can't open file !”,然后结束程序的执行

  17. 【例10.4】调用fscanf函数,按格式读取例10.3所建文件d:\cwz\b.txt中的学生成绩,并在终端屏幕上输出最高成绩【例10.4】调用fscanf函数,按格式读取例10.3所建文件d:\cwz\b.txt中的学生成绩,并在终端屏幕上输出最高成绩

  18. #include <stdio.h> main( ) { FILE *fp=NULL; int a=0, max=0; fp=fopen("d:\\cwz\\b.txt","r"); if( fp == NULL ) { printf("Can't open file !\n"); exit(0); } while( feof(fp) == 0 ) { fscanf(fp,"%d", &a); printf("%4d", a ); if ( max < a ) max=a; } printf("\n max=%d\n", max); fclose(fp); } 若非文件尾,继续 到文件尾时,feof(fp)为1

  19. 课堂练习 • 键盘输入若干个身份证号码(以“#”结束输入)存放在文件“ID_card.txt”中。 • 假设文件“ID_card.txt”中已存放若干身份证号码,输入当前日期,给那天生日的人显示贺词。

  20. 教学总结 本次课的讲授内容是: 1.文件、文件指针的概念。 2.文件的打开、关闭、读写函数。 3.exit函数。

  21. 作业 • P312 训练10.1、训练10.2 预习 • 例10.5 ~例10.7

  22. (第22次课) 【例10.5】假设学生基本情况包括学号和一门课成绩,从键盘输入若干学生的学号和成绩,写入文件d:\cwz\stu01.txt中,用-1结束成绩输入

  23. #include <stdio.h> struct aaa { char num[10]; int s; };

  24. main( ) { struct aaa stu={0}; FILE *fp=NULL; fp=fopen("d:\\cwz\\stu01.txt","w"); if( fp == NULL ) { printf("Can't open file !\n"); exit(0);} scanf("%s%d", stu.num, &stu.s ); while ( stu.s != -1) { fprintf(fp,"%10s%4d\n",stu.num,stu.s); scanf("%s%d", stu.num, &stu.s ); } fclose(fp); }

  25. stu01.txt的内容: 1001 67 1002 79 1003 99 1004 100 1005 87 1001 67 1002 79 1003 99 1004 100 1005 87 1 –1

  26. 【例10.6】编写程序从例10.5所建文件d:\cwz\stu01.txt中读取所有学生数据,输出成绩最高的学生信息【例10.6】编写程序从例10.5所建文件d:\cwz\stu01.txt中读取所有学生数据,输出成绩最高的学生信息

  27. #include <stdio.h> #define N 50 struct aaa { char num[10]; int s; };

  28. main( ) { int k=0,i=0,n=0; FILE *fp=NULL; struct aaa stu[N]={0}; fp=fopen("d:\\cwz\\stu01.txt","r"); if( fp == NULL ) { printf("Can't open file !\n"); exit(0); } while( feof(fp) == 0 ) {fscanf(fp,"%10s%4d\n", stu[n].num,&stu[n].s); printf("%10s%4d\n",stu[n].num,stu[n].s); n++; } printf("\n");

  29. k=0; for(i=0; i<n; i++) if( stu[k].s<stu[i].s) k=i; printf("%10s%4d\n", stu[k].num,stu[k].s); fclose(fp); }

  30. 【例10.7】编写程序修改例10.5所建文件d:\cwz\stu01.txt中最后一个学生的信息【例10.7】编写程序修改例10.5所建文件d:\cwz\stu01.txt中最后一个学生的信息

  31. #include <stdio.h> #define N 50 struct aaa { char num[10]; int s; };

  32. main( ) { int i=0,n=0; struct aaa stu[N]={0}; FILE *fp=NULL; fp=fopen("d:\\cwz\\stu01.txt","r"); if( fp == NULL ) { printf("Can't open file !\n"); exit(0); } while( feof(fp) == 0 ) {fscanf(fp,"%s%d\n",stu[n].num,&stu[n].s); printf("%10s%4d\n",stu[n].num,stu[n].s); n++; } fclose(fp);

  33. printf("Input: data: "); scanf("%s%d",stu[n-1].num,&stu[n-1].s); fp=fopen("D:\\cwz\\STU01.TXT","w"); if( fp == NULL ) { printf("Can't open file !\n"); exit(0); } for(i=0; i<n; i++) fprintf(fp,"%10s%4d\n", stu[i].num,stu[i].s ); fclose(fp); }

  34. 读写文件方式:顺序读写方式 随机读写方式

  35. 课堂练习 • 贯穿实例: 【实例13】用文件,编写实现电子通讯录功能的程序。

  36. 教学总结 本次课的讲授内容是: 1.修改文件中内容。 2.完善贯穿实例。

  37. 作业 • 继续完善电子通讯录管理系统功能,为课程设计课程做准备

  38. 上机实验(第8次实验) • 完成训练10.1 • 完成训练10.2

  39. 补充实验 编写程序实现如下功能: (1)调用myin函数,在D盘根下建立一个文件名为“file1.dat”的文件,从键盘输入以下几条记录存放在该文件中。 姓名数学英语 Lihua 92 80 Chenhao 82 78 Mali 90 86

  40. 补充实验 (2)调用mycal函数,从文件“file1.dat”中读取所有信息,并计算每位学生的总分,将所有这些记录(包括总分)写到文件“file2.dat”中,同时在屏幕上输出。 (3)调用排序函数mysor,从文件“file2.dat”中读取所有信息,并按总分降序顺序在屏幕上显示。

More Related