#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_TRUNC truncate file size to zero while open, // 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 intopen(constchar *pathname, int flags); intopen(constchar *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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#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 intclose(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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#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_tread(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
1 2 3 4 5 6 7 8 9 10
#include<unistd.h> // similar to read a file ssize_twrite(int fd, constvoid *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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#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_tlseek(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
1 2 3
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#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 inttruncate(constchar *path, off_t length);
// if return value is -1, you can then call to print the error message #include<stdio.h> perror("truncate");
#include<sys/types.h> #include<sys/stat.h> #include<unistd.h> // get file status // pathname: // the path of target file // statbuf: // the container to restore data // struct stat { // dev_t st_dev; /* ID of device containing file */ // ino_t st_ino; /* Inode number */ // mode_t st_mode; /* File type and mode */ // nlink_t st_nlink; /* Number of hard links */ // uid_t st_uid; /* User ID of owner */ // gid_t st_gid; /* Group ID of owner */ // dev_t st_rdev; /* Device ID (if special file) */ // off_t st_size; /* Total size, in bytes */ // blksize_t st_blksize; /* Block size for filesystem I/O */ // blkcnt_t st_blocks; /* Number of 512B blocks allocated */ // // struct timespec st_atim; /* Time of last access */ // struct timespec st_mtim; /* Time of last modification */ // struct timespec st_ctim; /* Time of last status change */ // // #define st_atime st_atim.tv_sec // #define st_mtime st_mtim.tv_sec // #define st_ctime st_ctim.tv_sec // }; // return value: // return 0 for success, -1 for error intstat(constchar *pathname, struct stat *statbuf); // similar to stat intlstat(constchar *pathname, struct stat *statbuf);
// if return value is -1, you can then call to print the error message #include<stdio.h> perror("stat");
#include<unistd.h> // check permissions to the file for current process // pathname: // the file path // mode: // optional and limitless: // F_OK for file exists, // R_OK for read permission for process, // W_OK for write permission for process, // X_OK for execute permission for process // return value: // return 0 for all permission is available, -1 for other case and error intaccess(constchar *pathname, int mode);
// if return value is -1, you can then call to print the error message #include<stdio.h> perror("access");
// About more // $ man 2 access
修改文件属性
chmod
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<sys/stat.h> // change mode of file // pathname: // the file path // mode: // three digits of oct-based number // UMASK work here // return value: // return 0 for success, -1 for error intchmod(constchar *pathname, mode_t mode);
// if return value is -1, you can then call to print the error message #include<stdio.h> voidperror("chmod");
// About more // $ man 2 chmod
修改文件所有
chown
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<unistd.h> // change owner of file // pathname: // the file path // owner: // the UID of owner // group: // the GID of group // return 0 for success, -1 for error intchown(constchar *pathname, uid_t owner, gid_t group);
// if return value is -1, you can then call to print the error message #include<stdio.h> voidperror("chown");
// About more // $ man 2 chown
Linux 目录操作函数
切换工作目录
chdir
1 2 3
#include<unistd.h> intchdir(constchar *path); // $ man 2 chdir
#include<stdio.h> // rename or move a file or director intrename(constchar *oldpath, constchar *newpath); // $ man 2 rename
移除目录
rmdir、unlink、remove
1 2 3 4
#include<unistd.h> // remove a director intrmdir(constchar *pathname); // $ man 2 rmdir
1 2 3 4
#include<unistd.h> // delete a file intunlink(constchar *pathname); // $ man 2 unlink
1 2 3 4
#include<stdio.h> // remove a file or director intremove(constchar *pathname); // $ man 3 remove
遍历目录
opendir、readdir、seekdir、closedir
常用属性:
d_off:文件在当前目录的序号
d_name:文件名
d_reclen:文件名长度
1 2 3 4 5 6 7 8 9
#include<sys/types.h> #include<dirent.h> // open a director // name: // the file path // return value: // a pointer to DIR, or NULL for error DIR *opendir(constchar *name); // $ man 3 opendir
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<dirent.h> // read an item in director // dirp: // a pointer to DIR return by "opendir" // return value: // an item in director and restore in struct dirent, NULL for EOF or other errors // struct dirent { // ino_t d_ino; /* Inode number */ // off_t d_off; /* Current position in the director stream */ // unsigned short d_reclen; /* Length of this record */ // unsigned char d_type; /* Type of file; not supported by all filesystem types */ // char d_name[256]; /* Null-terminated filename */ // }; // It's similar to read a file, while read an item in director, the file seek will move to the next item. You can use "seekdir" to reset the file offset. structdirent *readdir(DIR *dirp); // $ man 3 readdir
1 2 3 4 5 6 7 8
#include<dirent.h> // set the current position of pointer // dirp: // a pointer to DIR return by "opendir" // loc: // new position voidseekdir(DIR *dirp, long loc); // $ man 3 seekdir
1 2 3 4 5 6 7 8 9
#include<sys/types.h> #include<dirent.h> // close a director // dirp: // a pointer to DIR return by "opendir" // return value: // return 0 for success, -1 for error intclosedir(DIR *dirp); // $ man 3 closedir
Linux 操作文件描述符
复制文件描述符
dup、dup2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<unistd.h> // duplicate a file descriptor // oldfd: // old file descriptor // return value: // new file descriptor, or -1 for error intdup(int oldfd); // duplicate a file descriptor // oldfd: // old file descriptor // newfd: // new file descriptor // return value: // newfd, or -1 for error // If newfd is occupied, it will close the file first. // It is same to reopen a same file in same flags. intdup2(int oldfd, int newfd); // $ man 2 dup
#include<unistd.h> #include<fcntl.h> // manipulate file descriptor: get file descriptor flags // fd: // file descriptor // cmd: // F_GETFD: get file descriptor flags // return value: // return file descriptor flags intfcntl(int fd, int cmd); // $ man 2 fcntl
修改文件状态标记
fcntl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<unistd.h> #include<fcntl.h> // manipulate file descriptor: set file descriptor flags // fd: // file descriptor // cmd: // F_SETFD: get file descriptor flags // 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_CREATE for creatable. intfcntl(int fd, int cmd, int flags); // $ man 2 fcntl