fopen 中文man页面

系统
函数 fopen 打开 一个 文件, 并且 分配 一个 流, 文件名 由 字符串 path 指定.

NAME

fopen, fdopen, freopen - 打开流

SYNOPSIS (总览)

#include <stdio.h>

FILE *fopen(const char *path, const char *mode);
FILE *fdopen(int fildes, const char *mode);
FILE *freopen(const char *path, const char *mode, FILE *stream);

DESCRIPTION 描述

函数 fopen 打开 一个 文件, 并且 分配 一个 流, 文件名 由 字符串 path 指定.

参数 mode 指向 一个 字符串, 该 字符串 用 下面 的 字符串 开头 (之后 可以有 附加的 字符):

r
为 读操作 打开 文本文件. 流 被定位于 文件 的 开始.
r+
为 读写操作 打开 文本文件. 流 被定位于 文件 的 开始.
w
为 写操作 创建 文本文件, 或者 将 已经 存在的 文件长度 截断为 零. 流 被定位于 文件 的 开始.
w+
为 读写操作 打开 文件. 如果 文件 不存在, 就 创建 它, 否则 将它 截断. 流 被定位于 文件 的 开始.
a
为 追加操作 (在文件尾写) 打开 文件. 如果 文件 不存在, 就 创建 它. 流 被定位于 文件 的 末尾.
a+
为 追加操作 (在文件尾写) 打开 文件. 如果 文件 不存在, 就 创建 它. 读文件的初始位置 是 文件 的 开始, 但是 写文件 总是 被追加到 文件 的 末尾.

可以 把 字母 ``b'' 添加到 字符串 mode 的 末尾, 或者 插到 上面 任何 两个字符的 字符串 的 中间. 这样 只是 为了 和 ANSI X3.159-1989 (``ANSI C'') 标准 严格 保持 兼容, 没有 实际的 效果; 在 所有遵循 POSIX 的 系统 中, ``b'' 都 被忽略, 包括 Linux. (其他系统 可能会 分别对待 文本文件 和 二进制文件, 如果 在 进行 二进制文件 的 I/O, 那么 添加 ``b'' 是个 好主意, 因为 你的程序 可能会 被移植到 非 Unix 环境中.)

任何 新建文件 的 访问模式 是 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH (0666), 并且 用 进程的 掩码值 umask 加以修改 (参见 umask(2)).

在 读/写流 上 可以 任意 顺序 混合 进行 读写操作. 注意 ANSI C 要求 在 输出和输入操作 之间 插入 一个 文件定位函数, 除非 输入操作遇到了 文件结束符. (如果 不是 这种情况, 那么 读操作 总是 返回 写操作 的 结果而不是 ***的 内容.) 因此, *** (在 Linux 中 有时 是必须的) 对 这样的流 的 写/读操作 之间 加入 一个 fseek 或是 fgetpos 操作. 这个操作 可以 是一个 空操作 (如 fseek(..., 0L, SEEK_CUR)), 只利用其 文件同步 这个 副效应.

用 追加方式 打开 文件 (a 作为 mode 的 ***个 字符) 将使得 所有后续的 写操作 发生在 文件末尾, 如同 之前 调用了

fseek(stream,0,SEEK_END);

一样.

fdopen 函数 将 一个流 和 已存在的 文件描述符 fildes 联系 起来. 流的 操作模式 mode (取值为 "r", "r+", "w", "w+", "a", "a+" 之一) 必须 与 该文件描述符 的 操作模式一致. 流的 定位标识 设置为 fildes 原有的值, 清除 错误标记 和 文件结束标记. 模式 "w" 或者 "w+" 不会 截断 文件. fdopen 不复制 文件描述符, 在关闭 fdopen 创建的 流 时, 也不关闭 该文件描述符. 对 共享内存对象 实施 fdopen 的 结果 没有定义.

freopen 函数 打开 用 path 说明 的 文件, 并且 和 stream 指定的流 联系 起来. 原来的流 (如果 存在的话) 被关闭. 参数 modefopen 中的 用法 一样. freopen 函数 主要的用处 是 改变 标准文本流 (stderr, stdin, 或 stdout) 联系 的 文件.

RETURN VALUE (返回值)

如果 操作 成功, fopen, fdopenfreopen 返回 一个 指向 文件对象 FILE 的 指针, 否则 返回 NULL 并 设置 全局变量 errno 来 指出 错误的发生.

ERRORS

EINVAL
fopen, fdopen, 或 freopen 提供的 参数 mode 无效.

fopen, fdopenfreopen 也有可能 失败 并置 errnomalloc(3) 指定的值.

fopen 也有可能 失败 并置 errnoopen(2) 指定的值.

fdopen 也有可能 失败 并置 errnofcntl(2) 指定的值.

freopen 也有可能 失败 并置 errnoopen(2), fclose(3) 和 fflush(3) 指定的值.

CONFORMING TO (标准参考)

fopenfreopen 函数 遵循 ANSI X3.159-1989 (``ANSI C'') 标准. fdopen 遵循 IEEE Std1003.1-1988 (``POSIX.1'') 标准.

SEE ALSO (另见)

open(2), fclose(3), fileno(3)

#p#

NAME

fopen, fdopen, freopen - stream open functions  

SYNOPSIS

#include <stdio.h>

FILE *fopen(const char *path, const char *mode);
FILE *fdopen(int fildes, const char *mode);
FILE *freopen(const char *path, const char *mode, FILE *stream);  

DESCRIPTION

The fopen function opens the file whose name is the string pointed to by path and associates a stream with it.

The argument mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.):

r
Open text file for reading. The stream is positioned at the beginning of the file.
r+
Open for reading and writing. The stream is positioned at the beginning of the file.
w
Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
w+
Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
a
Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.
a+
Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

The mode string can also include the letter ``b'' either as a last character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with ANSI X3.159-1989 (``ANSI C'') and has no effect; the ``b'' is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary files differently, and adding the ``b'' may be a good idea if you do I/O to a binary file and expect that your program may be ported to non-Unix environments.)

Any created files will have mode S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH (0666), as modified by the process' umask value (see umask(2)).

Reads and writes may be intermixed on read/write streams in any order. Note that ANSI C requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file. (If this condition is not met, then a read is allowed to return the result of writes other than the most recent.) Therefore it is good practice (and indeed sometimes necessary under Linux) to put an fseek or fgetpos operation between write and read operations on such a stream. This operation may be an apparent no-op (as in fseek(..., 0L, SEEK_CUR) called for its synchronizing side effect.

Opening a file in append mode (a as the first character of mode) causes all subsequent write operations to this stream to occur at end-of-file, as if preceded by an

fseek(stream,0,SEEK_END);

call.

The fdopen function associates a stream with the existing file descriptor, fildes. The mode of the stream (one of the values "r", "r+", "w", "w+", "a", "a+") must be compatible with the mode of the file descriptor. The file position indicator of the new stream is set to that belonging to fildes, and the error and end-of-file indicators are cleared. Modes "w" or "w+" do not cause truncation of the file. The file descriptor is not dup'ed, and will be closed when the stream created by fdopen is closed. The result of applying fdopen to a shared memory object is undefined.

The freopen function opens the file whose name is the string pointed to by path and associates the stream pointed to by stream with it. The original stream (if it exists) is closed. The mode argument is used just as in the fopen function. The primary use of the freopen function is to change the file associated with a standard text stream (stderr, stdin, or stdout).  

RETURN VALUE

Upon successful completion fopen, fdopen and freopen return a FILE pointer. Otherwise, NULL is returned and the global variable errno is set to indicate the error.  

ERRORS

EINVAL
The mode provided to fopen, fdopen, or freopen was invalid.

The fopen, fdopen and freopen functions may also fail and set errno for any of the errors specified for the routine malloc(3).

The fopen function may also fail and set errno for any of the errors specified for the routine open(2).

The fdopen function may also fail and set errno for any of the errors specified for the routine fcntl(2).

The freopen function may also fail and set errno for any of the errors specified for the routines open(2), fclose(3) and fflush(3).  

CONFORMING TO

The fopen and freopen functions conform to ANSI X3.159-1989 (``ANSI C''). The fdopen function conforms to IEEE Std1003.1-1988 (``POSIX.1'').  

SEE ALSO

open(2), fclose(3), fileno(3)

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-25 15:47:06

fopen中文man

2011-08-24 16:48:36

man中文man

2011-08-15 10:21:09

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-25 15:39:42

fcloseall中文man

2011-08-25 15:00:15

cfgetispeed中文man

2011-08-19 18:35:50

issue中文man

2011-08-25 17:03:51

pclose中文man

2011-08-25 17:40:25

setvbuf中文man

2011-08-23 14:21:16

poweroff中文man

2011-08-24 15:52:59

intro中文man

2011-08-23 13:40:31

2011-08-25 17:24:54

puts中文man

2011-08-25 18:34:55

ungetc中文man

2011-08-23 10:03:40

useradd中文man

2011-08-23 10:29:02

chpasswd中文man

2011-08-23 10:34:22

convertquot中文man

2011-08-23 15:39:34

rpmbuild中文man

2011-08-24 15:48:38

INSERT中文man
点赞
收藏

51CTO技术栈公众号