Every time looking for this for each parameter, reproduced an explanation, for easy access later.
Turn: https: //account.cnblogs.com/signin returnUrl = http% 3a% 2f% 2fi.cnblogs.com% 2fEditPosts.aspx% 3fopt% 3d1?
Open int (const char * path, access int, int MODE)
path to open the file path and name of the
access mode access, macro definitions and meanings are as follows:
of O_RDONLY opened read-only. 1
O_WRONLY 2 Open write only
O_RDWR 4 open reading and writing
may be selected the following modes and three or more phase basic modes:
the O_CREAT 0x0100 creates a file and opens
O_TRUNC 0x0200 to open an existing file and the file length is set to 0, other holding properties
O_EXCL 0x0400 unused
additional file open O_APPEND 0x0800
O_TEXT 0x4000 open the text file translation control character CR-LF
O_BINARY 0x8000 open binary characters, without CR-LF translation
mode parameter is used only in the access = O_CREAT embodiment, its value as follows:
S_IFMT 0xF000 file type mask
S_IFDIR 0x4000 directory
S_IFIFO 0x1000 FIFO dedicated
S_IFCHR 0x2000 special characters
S_IFBLK 0x3000 block special
S_IFREG 0x8000 only 0x0000
S_IREAD readable 0x0100
S_IWRITE 0x0080 write
S_IEXEC 0x0040 executable
FILE * fopen (char * filename, char * mode)
filename file name
mode open mode:
r read-only open a text file
rb read-only open a binary file
w Write only way to open a text file
wb only a binary file is opened for writing
a way to open a text file append
ab append a binary file opened
r + read-write open a text file
rb + read and write a binary file opened
Create a text file w + Read-write
wb + read-write mode to generate a binary file
a + readable and writable additionally opened a text file
ab + Read-write append a binary
difference between open and fopen: the
former are lower IO, which is a high-level IO.
The former returns a file descriptor, which returns a file pointer.
Unbuffered former, the latter buffer.
The former and read, write, etc. used in conjunction with the latter fread, fwrite used in conjunction.
The latter is based on the former's expansion comes, in most cases, by the latter.
a fopen implementation
#if defined (of _POSIX_SOURCE)
#include <SYS / types.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
#define the PMODE 0666
#define of O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define O_CREAT 0x010
#define O_TRUNC 0x020
#define O_APPEND 0x040
int _open(const char *path, int flags);
int _creat(const char *path, Mode_t mode);
int _close(int d);
FILE *
fopen(const char *name, const char *mode)
{
register int i;
int rwmode = 0, rwflags = 0;
FILE *stream;
int fd, flags = 0;
for (i = 0; __iotab[i] != 0 ; i++)
if ( i >= FOPEN_MAX-1 )
return (FILE *)NULL;
switch(*mode++) {
case 'r':
flags |= _IOREAD | _IOREADING;
rwmode = O_RDONLY;
break;
case 'w':
flags |= _IOWRITE | _IOWRITING;
rwmode = O_WRONLY;
rwflags = O_CREAT | O_TRUNC;
break;
case 'a':
flags |= _IOWRITE | _IOWRITING | _IOAPPEND;
rwmode = O_WRONLY;
rwflags |= O_APPEND | O_CREAT;
break;
default:
return (FILE *)NULL;
}
while (*mode) {
switch(*mode++) {
case 'b':
continue;
case '+':
rwmode = O_RDWR;
flags |= _IOREAD | _IOWRITE;
continue;
default:
break;
}
break;
}
if ((rwflags & O_TRUNC)
|| (((fd = _open(name, rwmode)) < 0)
&& (rwflags & O_CREAT))) {
if (((fd = _creat(name, PMODE)) > 0) && flags | _IOREAD) {
(void) _close(fd);
fd = _open(name, rwmode);
}
}
if (fd < 0) return (FILE *)NULL;
if (( stream = (FILE *) malloc(sizeof(FILE))) == NULL ) {
_close(fd);
return (FILE *)NULL;
}
if ((flags & (_IOREAD | _IOWRITE)) == (_IOREAD | _IOWRITE))
flags &= ~(_IOREADING | _IOWRITING);
stream->_count = 0;
stream->_fd = fd;
stream->_flags = flags;
stream->_buf = NULL;
__iotab[i] = stream;
return stream;
}