冯毅《程序设计基础》c014-c10.pptVIP

  • 4
  • 0
  • 约1.67万字
  • 约 28页
  • 2018-12-02 发布于浙江
  • 举报
冯毅《程序设计基础》c014-c10

* 文本文件特点:行结束标志由一个回车符和一个换行符组成 读文件时:将回车和换行合并成一个换行 写文件时:将换行符展开成回车和换行两个符号 suitable for character operation * Old C version,there are two kinds of management method. 1. Buffer file high-level I/O ,ASCII file 2.non-buffer file low-level I/O,binary file ANSI C,there is only buffer file 特点:减少读写次数,提高速度和数据管理质量 * a data structure in the C program which represents the file Details of the struct are private to the standard C I/O library routines C program can operate directly on variables (data in main memory), so… Need to make a connection between the data on the disk and variables in main memory * r+ :以读、写方式打开一个已经存在的文件,文件指针指向其首部 w+ :建立一个新文件以读、写,文件指针指向其首部 a+ :以读、写方式打开一个已经存在的文件,文件指针指向其尾部 a/a+:向文本/二进制文件尾追加数据,若文件不存在,建立 System defines three file pointers automatically. stdin , stdout , stderr. They are constants, however,not variables,so it is not possible to assign to them. Opening a file: making a connection between the operating system (file name) and the C program (file variable) Files stdin/stdout (used by scanf/printf) are automatically opened connected to the keyboard and display * 因为fclose先把缓冲区数据输出 到磁盘文件,然后才释放文件指针 After having used the file ,it must be closed. int fclose(FILE *stream) fclose flushes any unwritten data for stream, discards any unread buffered input, frees any automatically allocated buffer, then closes the stream. * 1.若文件在当前目录下: fp=fopen(aa.txt,r); 2.若文件不在当前目录下: fp=fopen(d:\\fengyi\\bkc\\aa.txt,r); 2.若从键盘输入带路径文件名: char infile[30]; scanf(%s,infile); fp=fopen(infile,r); 必须输入:d:\fengyi\bkc\aa.txt * 第一个参数可以是:字符串常量、char数组名、char指针 * 说明:输入时,要将ASCII码转换成二进制;输出时,要将二进制转换成ASCII码 //将i和t按%d,%6.2f格式输出到fp文件 //若文件中有3,4.5 ,则将3送入i, 4.5送入t When we input the datum,convert ASCII to binary. When we output datum,convert binary to ASCII. Spend much time,seldom to use them End of File (EOF) Status EOF: a special status value Returned by scanf and fscanf when end of data is reached defined in stdio.h #define EOF (some negative v

文档评论(0)

1亿VIP精品文档

相关文档