UNIX编程(3)-文件IO
1.open函数 #include <fcntl.h> int open(const char *pathname, int oflag, .../*mode_t mode */);
oflag参数: O_RDONLY 只读打开 O_WRONLY 只写打开 O_RDWR 读写打开 这三个必须指定一个
下面是可选的: O_APPEND 每次写时都追加到文件的尾端 O_CREAT 若此文件不存在,则创建它 O_EXCL 如果同时指定了O_CREAT, 而文件已经存在,则会出错 O_TRUNC 如果此文件存在,而且为只写或读写成功打开,则将其长度截短为0 O_NOCTTY O_NONBLOCK O_DSYNC 使每次write等待物理I/O操作完成,但是如果写操作并不影响读取刚写入的数据,则不等待文件属性被更新 O_RSYNC 使每次一个以文件描述符作为参数的read操作等待,直至任何对文件同一 部分进行的未决写操作都完成 O_SYNC 使每次write等待物理I/O操作完成,包括由write操作引起的文件属性更新所需的IO 2.create函数 #include <fcntl.h> int creat(const char *pathname, mode_t mode); 等价于 open(pathname, O_WRONLY|O_CREAT|O_TRUNC, mode); 3.close函数 关闭一个打开的文件 #include <unistd.h> int close(int filedes); 4.lseek函数 #include <unistd.h> off_t lseek(int filedes, off_t offset, int whence); whence取值说明: SEEK_SET 则将该文件的偏移量设置为距文件开始处offset个字节 SEEK_CUR 则将该文件的偏移量设置为其当前值加offset,offset可正负 SEEK_END 则将该文件的偏移量设置为文件长度加offset,offset可正负
测试能否对标准输入设置偏移量
#include "apue.h"
int
main(void)
{
if (lseek(STDIN_FILENO, 0, SEEK_CUR) == -1)
printf("cannot seek\n");
else
printf("seek OK\n");
exit(0);
}
创建一个具有空洞的文件
#include "apue.h"
#include <fcntl.h>
char buf1[] = "abcdefghij";
char buf2[] = "ABCDEFGHIJ";
int
main(void)
{
int fd;
if ((fd = creat("file.hole", FILE_MODE)) < 0)
err_sys("creat error");
if (write(fd, buf1, 10) != 10)
err_sys("buf1 write error");
/* offset now = 10 */
if (lseek(fd, 16384, SEEK_SET) == -1)
err_sys("lseek error");
/* offset now = 16384 */
if (write(fd, buf2, 10) != 10)
err_sys("buf2 write error");
/* offset now = 16394 */
exit(0);
}
5.read函数 #include <unistd.h>
ssize_t read(int filedes, void *buf, size_t nbytes); 6.write函数 #include <unistd.h>
ssize_t write(int filedes, const void *buf, size_t nbytes); 将标准输入复制到标准输出
#include "apue.h"
#define BUFFSIZE 4096
int
main(void)
{
int n;
char buf[buffsize];
while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
if (write(STDOUT_FILENO, buf, n) != n)
err_sys("write error");
if (n < 0)
err_sys("read error");
exit(0);
}
7.pread和pwrite函数 #include <unistd.h>
ssize_t pread(int filedes, void *buf, size_t nbytes, off_t offset);
ssize_t pwrite(int filedes, const void *buf, size_t nbytes, off_t offset); 相当于顺序调用lseek和read或write,但pread和pwrite是原子操作
8.dup和dup2函数 复制一个现存的文件描述符
9.sync,fsync和fdatasync函数
#include <unistd.h>
int fsync(int filedes);
int fdatasync(int filedes);
void sync(void); sync函数只是将所有修改过的块缓冲区排入写队列,然后就返回,并不等待实际写磁盘操作结束 fsync函数只对由文件描述符filedes指定单一文件起作用,并且等待写磁盘操作结束 fdatasync类似于fsync,但他只影响文件的数据部分。fsync还会同步更新文件的属性。
10.fcntl函数 #include <fcntl.h>
int fcntl(int filedes, int cmd, ... /* int arg */ ); cmd说明: F_DUPFD 复制一个现有的描述符 F_GETFD 复制文件描述符 F_SETFD 设置文件描述符 F_GETFL 对应于filedes的文件状态标志作为函数值返回 F_SETFL 将文件状态标志设置为第三个参数值 F_GETOWN 取当前接受SIGIO和SIGURG信号的进程ID或进程组ID F_SETOWN 设置接受SIGIO和SIGURG信号的进程ID或进程组ID 对于指定的描述符打印文件标志
#include "apue.h"
#include <fcntl.h>
int
main(int argc, char *argv[])
{
int val;
if (argc != 2)
err_quit("usage: a.out <descriptor#>");
if ((val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0)
err_sys("fcntl error for fd %d", atoi(argv[1]));
switch (val & O_ACCMODE) {
case O_RDONLY:
printf("read only");
break;
case O_WRONLY:
printf("write only");
break;
case O_RDWR:
printf("read write");
break;
default:
err_dump("unknown access mode");
}
if (val & O_APPEND)
printf(", append");
if (val & O_NONBLOCK)
printf(", nonblocking");
#if defined(O_SYNC)
if (val & O_SYNC)
printf(", synchronous writes");
#endif
#if !defined(_POSIX_C_SOURCE) && defined(O_FSYNC)
if (val & O_FSYNC)
printf(", synchronous writes");
#endif
putchar('\n');
exit(0);
}
对一个文件描述符打开一个或多个文件状态标志
#include "apue.h"
#include <fcntl.h>
void
set_fl(int fd, int flags) /* flags are file status flags to turn on */
{
int val;
if ((val = fcntl(fd, F_GETFL, 0)) < 0)
err_sys("fcntl F_GETFL error");
val |= flags; /* turn on flags */
if (fcntl(fd, F_SETFL, val) < 0)
err_sys("fcntl F_SETFL error");
}
11.ioctl函数 #include <unistd.h> /* System V / #include <sys/ioctl.h> / BSD and Linux / #include <stropts.h> / XSI STREAMS */
int ioctl(int filedes, int request, ...);
ioctl函数是IO操作的杂物箱。