一、creat和open
#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);
/* 是的,这个函数名的确少个e,Ken Thompson曾开玩笑说漏掉这个字母是他设计Unxi中最后悔的事。 */
int creat(const char *pathname, mode_t mode);
int openat(int dirfd, const char *pathname, int flags);
int openat(int dirfd, const char *pathname, int flags, mode_t mode);
打开和创建文件都可以使用系统调用函数open()
,该函数的作用是建立一个文件描述符,其他的函数可以通过文件描述符对指定文件进行读取与写入的操作。creat与open的返回值均为文件描述符,如果打开文件成功返回一个正整数,否则返回-1并设置errno的值。
函数参数如下:
-
pathname
:表示文件名,包含完整路径,若不包含路径则默认当前路径。 -
flags
:表示的是文件的打开方式,可以用下列一个或多个常量进行或运算构成:①下面三个常量必须指定一个且只能指定一个:
O_RDONLY
:只读打开;
O_WRONLY
:只写打开;
O_RDWR
:读写打开。②下面的常量是可以选择的:
O_CREAT
:若此文件不存在则创建它,但这个时候要使用第三个参数mode,用其指定该文件的访问权限位;
O_APPEND
:在打开文件后将读写位置置于文件尾;
O_TRUNC
:如果被打开的文件存在并是以可写的方式打开的,则清空文件原有内容。 -
mode
:表示文件权限标志,常用0755
,表示所有者可读可写可执行,群组和其他用户可读可执行。
二、wirte
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
write()
函数把length个字节从buf指向的缓冲区中写到文件描述符fd所指向的文件中,返回值为实际写入的字节数,如果写入失败返回-1并设置errno的值。
三、lseek
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
lseek()
函数将文件读写指针相对whence移动offset个字节。操作成功时,返回文件指针相对于文件头的位置。
函数参数如下:
-
fd
:一个已经打打开的文件的文件描述符。 -
offset
:相对于whence的偏移量,可正可负,具体含义取决于whence。 -
whence
:表示的是偏移的基准位置,有以下三种取值:SEEK_SET
:基准位置设置为文件头;
SEEK_CUR
:基准位置即为读写指针当前位置;
SEEK_END
:基准位置设置为文件尾。
四、read
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
read()
函数从文件描述符fd所指定的文件中读取length个字节到buf所指向的缓冲区中,返回值为实际读取的字节数,如果读取失败返回-1并设置errno的值。
五、基本使用
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 1024
int main(int argc, char *argv[]) {
int fd = -1;
/* 参数检查 */
if (argc != 2) {
//通过命令行参数输入文件名
printf("Please input correct parameter.\n");
exit(1);
}
/* 创建文件 */
/* fd = creat(argv[1], 0755);
if (fd == -1) {
perror("Can't create this file");
exit(1);
} else {
printf("Create successfully, the fd of \"%s\" is %d.\n", argv[1], fd);
} */
/* 打开文件 */
fd = open(argv[1], O_RDWR | O_CREAT | O_APPEND, 0755);//读写打开,不存在则以755权限创建,读写指针移至末尾
if (fd == -1) {
perror("Can't open this file");
exit(1);
} else {
printf("Open successfully, the fd of \"%s\" is %d.\n", argv[1], fd);
}
/* 写入文件 */
char buffer[MAX_SIZE] = "";//用于存储待写入及读出的数据
int w_len = 0;//写长度
strcpy(buffer, "Hello World!");
w_len = write(fd, buffer, strlen(buffer));
if (w_len == -1) {
perror("Can't write this file");
exit(1);
} else {
printf("Write successfully, the write length in \"%s\" is %d.\n", argv[1], w_len);
}
/* 读取文件 */
int r_len = 0;//读长度
memset(buffer, 0, sizeof(buffer));
lseek(fd, 0, SEEK_SET);//读写指针移至文件头
r_len = read(fd, buffer, sizeof(buffer) - 1);
if (r_len == -1) {
perror("Can't read this file");
exit(1);
} else if (r_len == 0) {
printf("Read file end.\n");
} else {
printf("Read successfully, the read length in \"%s\" is %d.\n", argv[1], r_len);
printf("Read data:%s\n", buffer);
}
/* 获取文件长度 */
int file_len = 0;
file_len = lseek(fd, 0, SEEK_END);
printf("The length of \"%s\" is %d.\n", argv[1], file_len);
/* 关闭文件 */
close(fd);
return 0;
}
运行结果:
六、文件拷贝
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 1024
int main(int argc, char *argv[]) {
int src_fd, des_fd;
int r_byte, w_byte;
char buffer[BUFFER_SIZE];
/* 参数检查 */
if (argc != 3) {
fprintf(stdout, "Please input correct parameter.\n");
exit(1);
}
/* 打开源文件 */
src_fd = open(argv[2], O_RDONLY);
if (src_fd == -1) {
perror("Can't open source file");
exit(1);
}
/* 打开目标文件 */
des_fd = open(argv[1], O_WRONLY | O_CREAT, 0755);
if (des_fd == -1) {
perror("Can't open or create destination file");
exit(1);
}
/* 文件拷贝 */
lseek(des_fd, 0, SEEK_SET);
while (r_byte = read(src_fd, buffer, BUFFER_SIZE)) {
if (r_byte == -1) {
perror("Copy failed");
break;
} else {
w_byte = write(des_fd, buffer, r_byte);
if (w_byte != r_byte) {
perror("Copy failed");
}
}
}
fprintf(stdout, "A total of %d bytes were copied.\n", (int) lseek(src_fd, 0, SEEK_END));
/* 关闭文件 */
close(src_fd);
close(des_fd);
exit(0);
}
运行结果: