1.6.2 Linux 文件操作函数
前言
关于文件操作函数这一块主要用英文文档的形势书写,因为凉皮在写文档的时候发现Markdown
用起来太繁琐了。那么关于函数
这一块主要也是读文档就能解决的了。
关于文档可以使用Linux
命令man 2 xxx
查看,在后面也写了查看每个文档的命令。
还是需要再次声明的,Linux
的文件读写效率低于标准C库
的文件读写效率,而本文主要介绍Linux
文件读写函数,毕竟这是Linux
编程的基础篇嘛。在一般应用场合使用标准C库
文件读写函数更好,特别场合使用Linux
文件读写函数,至于为什么,凉皮在上篇文章中写到过。
打开文件
open
O_RDONLY
、O_WRONLY
、O_RDWR
、O_APPEND
、O_NONBLOCK
、O_CREAT
UMASK
:在后文的权限操作中,也会与UMASK
操作后处理。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// open a file
// pathname:
// the file path to open
// flags:
// must and exactly one:
// O_RDONLY for read only,
// O_WRONLY for write only,
// O_RDWR for read and write
// optional and limitless:
// O_APPEND for append mode,
// O_NONBLOCK for non-block,
// O_CREAT for creatable.
// mode:
// If O_CREATE flag is on, must follow with the key.
// Mode is a oct-based number and determine the limits of the new file.
// It includes three digits (such as 0664), for user - group - others.
// And each digit can be extend to three binary-bits, for read - write - execute.
// The finally mode will be (mode^~UMASK).
// * UMASK:
// A mask on the mode to ensure the file didn't give the inappropriate permission.
// Also you can change the value of UMASK, command "$ umask" to show the value.
// The default value is 0002. Because giving others write permission is inappropriate.
// return value:
// file descripor, or -1 for error
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("open"); // About more: $ man 3 perror
// About more
// $ man 2 open
关闭文件
close
#include <unistd.h>
// close a file
// file will automatic close while process ended
// fd:
// file description
// return value:
// return 0 for success, -1 for error
int close(int fd);
// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("close");
// About more
// $ man 2 close
读取文件
read
#include <unistd.h>
// read a file
// fd:
// file descriptor of a readable file
// buf:
// he container of data
// count:
// read size for each time
// return value:
// real read size of data, or -1 for error
// Because the rest size maybe smaller than ideal. It become zero if read the end of file.
ssize_t read(int fd, void *buf, size_t count);
// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("read");
// About more
// $ man 2 read
写入文件
write
#include <unistd.h>
// similar to read a file
ssize_t write(int fd, const void *buf, size_t count);
// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("write");
// About more
// $ man 2 write
文件指针
lseek
SEEK_SET
、SEEK_CUR
、SEEK_END
#include <sys/types.h>
#include <unistd.h>
// reposition read / write file offset
// fd:
// file descriptor
// offset:
// relative offset to whence
// whence:
// SEEK_SET for BOF, SEEK_CUR for now file offset, SEEK_END for EOF
// return value:
// absolute file offset
off_t lseek(int fd, off_t offset, int whence);
// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("lseek");
// About more
// $ man 2 lseek
int offset = lseek(fd, 0, SEEK_CUR); // get now file offset
lseek(fd, offset, SEEK_SET); // reposition to absolute offset
lseek(fd, 0, SEEK_END); // reposition to the end of file
文件截断
truncate
#include <unistd.h>
#include <sys/types.h>
// resize the file
// path:
// the file path
// length:
// new length of file
// append \x00 if become larger and cut down the rest tail if become smaller
// return value:
// return 0 for success, -1 for error
int truncate(const char *path, off_t length);
// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("truncate");
// About more
// $ man 2 truncate