文件编程小结(一)
项目目的:掌握并懂得如何应用常用的linux文件I/O函数:open、read、write、close 、lseek。
open:
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);
/*
对于 open 函数来说,第三个参数(...)仅当创建新文件时才使用,用于指定文件的访问权限位(access permission bits)。pathname 是待打开/创建文件的路径名(如 C:/cpp/a.cpp);oflag 用于指定文件的打开/创建模式,这个参数可由以下常量(定义于 fcntl.h)通过逻辑或构成。
返回值:成功则返回文件描述符,否则返回 -1
O_RDONLY 只读模式
O_WRONLY 只写模式
O_RDWR 读写模式
打开/创建文件时,至少得使用上述三个常量中的一个。以下常量是选用的:
O_APPEND 每次写操作都写入文件的末尾
O_CREAT 如果指定文件不存在,则创建这个文件
O_EXCL 如果要创建的文件已存在,则返回 -1,并且修改 errno 的值
O_TRUNC 如果文件存在,并且以只写/读写方式打开,则清空文件全部内容
O_NOCTTY 如果路径名指向终端设备,不要把这个设备用作控制终端。
O_NONBLOCK 如果路径名指向 FIFO/块文件/字符文件,则把文件的打开和后继 I/O设置为非阻塞模式(nonblocking mode)
*/
read:
SYNOPSIS
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
/*DESCRIPTION
read() attempts to read up to count bytes from file descriptor fd into
the buffer starting at buf.
If count is zero, read() returns zero and has no other results. If
count is greater than SSIZE_MAX, the result is unspecified.
RETURN VALUE
On success, the number of bytes read is returned (zero indicates end of
file), and the file position is advanced by this number. It is not an
error if this number is smaller than the number of bytes requested;
this may happen for example because fewer bytes are actually available
right now (maybe because we were close to end-of-file, or because we
are reading from a pipe, or from a terminal), or because read() was
interrupted by a signal. On error, -1 is returned, and errno is set
appropriately. In this case it is left unspecified whether the file
position (if any) changes.
*/
write:
SYNOPSIS
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
/*
DESCRIPTION
write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd.
The number of bytes written may be less than count if, for example, there is insufficient space on the underlying physical medium, or the
RLIMIT_FSIZE resource limit is encountered (see setrlimit(2)), or the call was interrupted by a signal handler after having written less
than count bytes. (See also pipe(7).)
For a seekable file (i.e., one to which lseek(2) may be applied, for example, a regular file) writing takes place at the current file off‐
set, and the file offset is incremented by the number of bytes actually written. If the file was open(2)ed with O_APPEND, the file offset
is first set to the end of the file before writing. The adjustment of the file offset and the write operation are performed as an atomic
step.
POSIX requires that a read(2) which can be proved to occur after a write() has returned returns the new data. Note that not all file sys‐
tems are POSIX conforming.
RETURN VALUE
On success, the number of bytes written is returned (zero indicates nothing was written). On error, -1 is returned, and errno is set appro‐
priately.
If count is zero and fd refers to a regular file, then write() may return a failure status if one of the errors below is detected. If no
errors are detected, 0 will be returned without causing any other effect. If count is zero and fd refers to a file other than a regular
file, the results are not specified.
*/
lseek:
SYNOPSIS
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
/*
/DESCRIPTION
The lseek() function repositions the offset of the open file associated with the file descriptor fd to the argument offset according to the
directive whence as follows:
SEEK_SET
The offset is set to offset bytes.
SEEK_CUR
The offset is set to its current location plus offset bytes.
SEEK_END
The offset is set to the size of the file plus offset bytes.
RETURN VALUE
Upon successful completion, lseek() returns the resulting offset location as measured in bytes from the beginning of the file. On error,
the value (off_t) -1 is returned and errno is set to indicate the error.
*/
close:
SYNOPSIS
#include <unistd.h>
int close(int fd);
/*
DESCRIPTION
close() closes a file descriptor, so that it no longer refers to any file and may be reused. Any record locks (see fcntl(2)) held on the
file it was associated with, and owned by the process, are removed (regardless of the file descriptor that was used to obtain the lock).
If fd is the last file descriptor referring to the underlying open file description (see open(2)), the resources associated with the open
file description are freed; if the descriptor was the last reference to a file which has been removed using unlink(2) the file is deleted.
RETURN VALUE
close() returns zero on success. On error, -1 is returned, and errno is set appropriately.
*/
看完这些,不妨试一试小项目检验一下掌握程度:实现linux 的cp指令的代码