- 2
- 0
- 约2.78千字
- 约 50页
- 2018-02-28 发布于天津
- 举报
*;Chap 12 文件;本章要点;12.1 将短句“Hello World”写入到文件;#include stdio.h
#include stdlib.h
int main(void)
{
FILE *fp; /* 定义文件指针*/
if( ( fp = fopen(f1.txt, w) ) == NULL){ /* 打开文件 */
printf(File open error!\n);
exit(0);
}
fprintf( fp, %s, Hello World! ); /* 写文件 */
if( fclose( fp ) ){ /* 关闭文件 */
printf( Can not close the file!\n );
exit(0);
}
return 0;
};#include stdio.h
#include stdlib.h
int main(void)
{ FILE *fp; /* 定义文件指针*/
char *chp=“hello world”; /* 定义字符串指针*/
if( ( fp = fopen(f1.txt, w) ) == NULL){ /* 打开文件 */
printf(File open error!\n);
exit(0);
}
printf(please input a string\n);
gets(chp);
fprintf( fp, %s, chp); /* 写文件 */
if( fclose( fp ) ){ /* 关闭文件 */
printf( Can not close the file!\n );
exit(0);
}
return 0;
};12.1.2 文件的概念;12.1.3 文本文件和二进制文件 ;如 int型数10000;12.1.4 缓冲文件系统;自定义类型(typedef);FILE:结构类型
用 typedef 定义,stdio.h
typedef struct{
short level; /* 缓冲区使用量 */
unsigned flags; /* 文件状态标志 */
char fd; /* 文件描述符 */
short bsize; /* 缓冲区大小 */
unsigned char *buffer; /* 文件缓冲区的首地址 */
unsigned char *curp; /* 指向文件缓冲区的工作指针 */
unsigned char hold; /* 其他信息 */
unsigned istemp;
short token;
} FILE;;3. 文件类型指针;12.2 读取学生成绩文件;12.2.1 程序解析;12.2.2 打开文件;文件打开方式;文件读写与打开方式;12.2.3 关闭文件;12.3 文件复制;例12-3 源程序;打开多个文件;文件读写函数;12.3.2 字符读写函数fgetc和fputc;字符读写函数fgetc和fputc;从键盘输入10个字符,写到文件 f2.txt 中,再重新读出,并在屏幕上显示验证。
键盘输入 ? 写入文件 f2.txt ? 读文件 ? 屏幕显示的过程
键盘输入 ?? f2.txt时,文件按写方式打开。
f2.txt ? 显示到屏幕时,文件按读方式打开。
读和写是两种不同的操作, f2.txt 分别被打开和关闭两次。
;源程序;12.3.3 字符串方式读写函数fgets和fputs;字符串方式读写函数fgets和fputs;将字符串apple, grape, pear 写入到磁盘文件f12-5.txt中,然后再从该文件中读出,显示到屏幕。
int main(void)
{ FI
原创力文档

文档评论(0)