Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2084290
  • 博文数量: 414
  • 博客积分: 10312
  • 博客等级: 上将
  • 技术积分: 4921
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-31 01:49
文章分类

全部博文(414)

文章存档

2011年(1)

2010年(29)

2009年(82)

2008年(301)

2007年(1)

分类: C/C++

2010-07-04 03:57:26

open a file and associate stream
 

#include

FILE *fopen(const char *filename, const char *type);

FILE *fopen64(const char *filename, const char *type);


The fopen() function opens the file named by filename and associates a stream with it. This function returns a pointer to the FILE structure associated with the stream.

Opening the file for reading fails if the file does not exist or cannot be read.

Opening the file for append causes all subsequent writes to the file to be forced to the then current end of file, regardless of intervening calls to the function.

When a file is opened for update, both input and output may be performed on the associated stream. However, output may not be directly followed by input without an intervening call to the function or to a file positioning function (, , or ), and input may not be directly followed by output without an intervening call to a file positioning function unless the input operation encounters end-of-file.

When a stream is opened, it is fully buffered only if it does not refer to an interactive device. The error and end-of-file indicators are cleared for the stream.

The fopen64() function is identical to the fopen() function except that the underlying file descriptor is created with the O_LARGEFILE flag set. The fopen64() function is a part of the large-file extensions.


filename 

Points to a character string that contains the name of the file to be opened.

type 

Indicates a character string that specifies the desired type of file access. The first character of this string must be one of r (open for reading), w (open for writing), or a (open for writing at the end of file —append).

After the first character, you can specify several optional modifiers: b, open in binary mode; t, open in text mode, +, open for update (both reading and writing); S, optimize performance for sequential access; R, optimize performance for random access; D, file should be deleted when the last file descriptor is closed; T, optimize performance for short-lived temporary files.


On failure, the fopen() and fopen64() functions return a NULL pointer and errno is set to one of the following values:

EACCES 

Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by mode are denied, or the file does not exist and write permission is denied for the parent directory of the file to be created.

EINVAL 

The value of the type argument is invalid.

EISDIR 

The named file is a directory, and type requires write access.

EMFILE 

Too many file descriptors are open in the calling process.

ENOENT 

A component of filename does not name an existing file, or filename is an empty string.

ENOMEM 

Insufficient storage space is available.

ENOTDIR 

A component of the path prefix is not a directory.

EOVERFLOW 

The named file is a regular file and the size of the file cannot be represented correctly in an object of type off_t.

ETXTBSY 

The file is an executable that is in use, and type requires write access.


ANSI/ISO 9899-1990, with exceptions.

fopen64(): Large File Specification, revision 1.5.


MT-Safe.


The modifiers D, T, R, S, and t are MKS Toolkit UNIX APIs extensions, and are not guaranteed to be portable. If neither b nor t is specified, the determination of binary or text is made by examining the current state of the BINARY_OFLAG (see for details).


MKS Toolkit for Professional Developers
MKS Toolkit for Enterprise Developers
MKS Toolkit for Enterprise Developers 64-Bit Edition


阅读(3564) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~