C语言标准IO文件的打开和关闭

24 篇文章 1 订阅
17 篇文章 0 订阅

二.标准IO

1.文件流和缓冲区

标准IO是C标准库(C语言提供的可以直接使用的库)提供的一组访问文件的函数

标准IO使用文件流(结构体指针FILE *)代表一个打开的文件,需要包含stdio.h头文件

stdio.h

49 typedef struct _IO_FILE FILE;

libio.h

273 struct _IO_FILE {
274 int _flags; /* High-order word is _IO_MAGIC; rest is flags. /
275 #define _IO_file_flags _flags
276
277 /
The following pointers correspond to the C++ streambuf protocol. /
278 /
Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. /
279 char
_IO_read_ptr; /* 读的位置*/
280 char* _IO_read_end; /* 读缓冲结束位置 /
281 char
_IO_read_base; /* 读缓冲起始位置 /
282 char
_IO_write_base; /* 写缓冲的起始位置 /
283 char
_IO_write_ptr; /* 写的位置 /
284 char
_IO_write_end; /* 写缓冲的结束位置 /
285 char
_IO_buf_base; /* Start of reserve area. /
286 char
_IO_buf_end; /* End of reserve area. /
287 /
The following fields are used to support backing up and undo. */
288 char _IO_save_base; / Pointer to start of non-current get area. */
289 char _IO_backup_base; / Pointer to first valid character of backup area */
290 char _IO_save_end; / Pointer to end of non-current get area. /
291
292 struct _IO_marker _markers;
293
294 struct _IO_FILE _chain;
295
296 int _fileno;
297 #if 0
298 int _blksize;
299 #else
300 int _flags2;
301 #endif
302 _IO_off_t _old_offset; /
This used to be _offset but it’s too small. /
303
304 #define __HAVE_COLUMN /
temporary /
305 /
1+column number of pbase(); 0 is unknown. /
306 unsigned short _cur_column;
307 signed char _vtable_offset;
308 char _shortbuf[1];
309
310 /
char
_save_gptr; char
_save_egptr; */
311
312 _IO_lock_t *_lock;
313 #ifdef _IO_USE_OLD_IO_FILE
314 };

标准IO带缓冲区,读写操作都是在缓冲区中进行,缓冲区根据某种时机再和内核中的磁盘进行同步

根据缓冲机制,可以将缓冲分为以下几类:

无缓冲:不使用缓冲区

行缓冲:遇到换行符就同步缓冲

全缓冲:缓冲区满了就同步缓冲

注:标准输入输出属于行缓冲,标准错误属于无缓冲

在C语言中,标准输入输出,标准错误的文件流指针已经被定义,在程序运行时已经被打开

    标准输入            stdin

    标准输出            stdout

    标准错误            stderr

标准IO的缓冲可以通过setbuf/setvbuf来设置

缓冲区大小选择为512bytes—4Kbytes之间效率最高

fflush函数可以强制刷新缓冲区

2.文件的打开和关闭

参数:

path:要打开文件的路径

mode:打开方式

    r:以只读方式打开文件,文件必须存在

    r+:以读写方式打开文件,文件必须存在,写的位置在文件开头

    w:以只写方式打开文件,文件不存在就创建,存在清空文件内容

    w+:以读写方式打开文件,文件不存在就创建,存在清空文件内容

    a:以只写方式打开文件,文件不存在创建,存在就在末尾追加(写的位置在文件末尾)

    a+:以读写方式打开文件,文件不存在创建,存在就在末尾追加(写的位置在文件末尾)

返回值:

成功返回文件流指针,失败返回NULL(必须判断返回值)

关闭打开的文件

参数:

fopen的返回值(文件流指针)

二.标准IO

1.文件流和缓冲区

标准IO是C标准库(C语言提供的可以直接使用的库)提供的一组访问文件的函数

标准IO使用文件流(结构体指针FILE *)代表一个打开的文件,需要包含stdio.h头文件

stdio.h

49 typedef struct _IO_FILE FILE;

libio.h

273 struct _IO_FILE {
274 int _flags; /* High-order word is _IO_MAGIC; rest is flags. /
275 #define _IO_file_flags _flags
276
277 /
The following pointers correspond to the C++ streambuf protocol. /
278 /
Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. /
279 char
_IO_read_ptr; /* 读的位置*/
280 char* _IO_read_end; /* 读缓冲结束位置 /
281 char
_IO_read_base; /* 读缓冲起始位置 /
282 char
_IO_write_base; /* 写缓冲的起始位置 /
283 char
_IO_write_ptr; /* 写的位置 /
284 char
_IO_write_end; /* 写缓冲的结束位置 /
285 char
_IO_buf_base; /* Start of reserve area. /
286 char
_IO_buf_end; /* End of reserve area. /
287 /
The following fields are used to support backing up and undo. */
288 char _IO_save_base; / Pointer to start of non-current get area. */
289 char _IO_backup_base; / Pointer to first valid character of backup area */
290 char _IO_save_end; / Pointer to end of non-current get area. /
291
292 struct _IO_marker _markers;
293
294 struct _IO_FILE _chain;
295
296 int _fileno;
297 #if 0
298 int _blksize;
299 #else
300 int _flags2;
301 #endif
302 _IO_off_t _old_offset; /
This used to be _offset but it’s too small. /
303
304 #define __HAVE_COLUMN /
temporary /
305 /
1+column number of pbase(); 0 is unknown. /
306 unsigned short _cur_column;
307 signed char _vtable_offset;
308 char _shortbuf[1];
309
310 /
char
_save_gptr; char
_save_egptr; */
311
312 _IO_lock_t *_lock;
313 #ifdef _IO_USE_OLD_IO_FILE
314 };

标准IO带缓冲区,读写操作都是在缓冲区中进行,缓冲区根据某种时机再和内核中的磁盘进行同步

根据缓冲机制,可以将缓冲分为以下几类:

无缓冲:不使用缓冲区

行缓冲:遇到换行符就同步缓冲

全缓冲:缓冲区满了就同步缓冲

注:标准输入输出属于行缓冲,标准错误属于无缓冲

在C语言中,标准输入输出,标准错误的文件流指针已经被定义,在程序运行时已经被打开

    标准输入            stdin

    标准输出            stdout

    标准错误            stderr

标准IO的缓冲可以通过setbuf/setvbuf来设置

缓冲区大小选择为512bytes—4Kbytes之间效率最高

fflush函数可以强制刷新缓冲区

2.文件的打开和关闭

参数:

path:要打开文件的路径

mode:打开方式

    r:以只读方式打开文件,文件必须存在

    r+:以读写方式打开文件,文件必须存在,写的位置在文件开头

    w:以只写方式打开文件,文件不存在就创建,存在清空文件内容

    w+:以读写方式打开文件,文件不存在就创建,存在清空文件内容

    a:以只写方式打开文件,文件不存在创建,存在就在末尾追加(写的位置在文件末尾)

    a+:以读写方式打开文件,文件不存在创建,存在就在末尾追加(写的位置在文件末尾)

返回值:

成功返回文件流指针,失败返回NULL(必须判断返回值)

关闭打开的文件

参数:

fopen的返回值(文件流指针)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值