编写一个程序,打开一个现有文件,用O_APPEND标志写入,和 然后在写入一些数据之前寻找文件的开头。在哪里 数据出现在文件中?为什么呢?
这是我的代码:
main() {
int fd = open("test.txt", O_WRONLY | O_APPEND);
lseek(fd, 0, SEEK_SET);
write(fd, "abc", 3);
close(fd);
}
并且已经尝试过,发现数据已写入文件的末尾,我想了解为什么?因为我表示O_APPEND标志没有那么简单我认为
答案 0 :(得分:16)
当您使用O_APPEND
打开文件时,无论当前文件指针来自最新的lseek(2)
调用或最新的读/写操作,所有数据都会写入结尾。来自open(2)
documentation:
O_APPEND
该文件以追加模式打开。在每个write(2)
之前,文件偏移量位于文件的末尾,就像使用lseek(2)
一样。
如果要将数据写入文件的末尾,然后在以后开始写入数据,请在不使用O_APPEND
的情况下将其打开,使用fstat(2)
获取文件大小(st_size
成员在struct stat
)内,然后寻找该偏移来写结束。
答案 1 :(得分:1)
实际上,O_APPEND仅影响write
的行为,而不影响read
的行为。无论lseek
如何更改文件的当前位置,write
始终为append-only
。
当open
文件O_RDWR | O_APPEND
时,read
仍将从文件开头开始。
在open
(man 2 open
)的手册中,
O_APPEND 该文件以追加模式打开。在每次写入(2)之前,文件偏移量位于文件的末尾。
在write
(man 2 write
)的手册中,
如果设置了文件状态标志的O_APPEND标志,则为文件偏移量 应在每次写入之前设置到文件的末尾。
在Linux内核fs/ext4 syscall write
-> vfs_write
-> ext4_file_write_iter
中,
ext4_file_write_iter
会致电ext4_write_checks
您会找到设置pos
= file.size
/* FIXME: this is for backwards compatibility with 2.4 */
if (iocb->ki_flags & IOCB_APPEND)
iocb->ki_pos = i_size_read(inode);
pos = iocb->ki_pos;
以下演示可以验证它。
cat open_append.cc
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <string>
#include <iostream>
int main(int argc, char *argv[]) {
std::string path = "./test.txt";
std::string content = "hello_world";
std::string read_buf(content.size(), 0x0);
struct stat st_buf;
ssize_t bytes_read = -1;
ssize_t bytes_write = -1;
int ret = -1;
off_t cur_off = -1;
int fd = ::open(path.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0644);
if (fd < 0) {
std::cerr << "open err path " << path
<< " errno " << errno << std::endl;
return -1;
}
std::cout << "open ok path " << path
<< " fd " << fd << std::endl;
// Step 1 write some data into an empty file
bytes_write = ::write(fd, content.data(), content.size());
if (bytes_write < 0) {
std::cerr << "write err fd " << fd
<< " errno " << errno << std::endl;
goto out;
}
std::cout << "write ok fd " << fd
<< " data " << content
<< " nbytes " << bytes_write << std::endl;
::close(fd);
// Step 2 open the file again with O_APPEND
fd = -1;
fd = ::open(path.c_str(), O_CREAT | O_RDWR | O_APPEND, 0644);
if (fd < 0) {
std::cerr << "open again err path " << path
<< " errno " << errno << std::endl;
return -1;
}
std::cout << "open again ok path " << path
<< " fd " << fd << std::endl;
// Step 3 the current position of the file NOT affected by O_APPEND
cur_off = ::lseek(fd, 0, SEEK_CUR);
if (cur_off < 0) {
std::cerr << "lseek err SEEK_CUR fd " << fd
<< " errno " << errno << std::endl;
goto out;
}
// cur_off expected to be 0
std::cout << "lseek ok SEEK_CUR fd " << fd
<< " cur_off " << cur_off << std::endl;
// Step 4 the read will start from the beginning of the file
bytes_read = read(fd, (char*)read_buf.data(), content.size());
if (bytes_read < 0) {
std::cerr << "read err fd " << fd
<< " errno " << errno << std::endl;
goto out;
}
std::cout << "read ok fd " << fd
<< " data " << read_buf
<< " nbytes " << bytes_read << std::endl;
// Step 5 change the position to the half of the file size
cur_off = ::lseek(fd, content.size() / 2, SEEK_SET);
if (cur_off < 0) {
std::cerr << "lseek err SEEK_SET fd " << fd
<< " errno " << errno << std::endl;
goto out;
}
// cur_off expected to be content.size() / 2
std::cout << "lseek ok SEEK_SET fd " << fd
<< " cur_off " << cur_off << std::endl;
// Step 6 write will append data from the end of the file
// the current position is ignored
bytes_write = ::write(fd, content.data(), content.size());
if (bytes_write < 0) {
std::cerr << "append write err fd " << fd
<< " errno " << errno << std::endl;
goto out;
}
std::cout << "append write ok fd " << fd
<< " append data " << content
<< " append nbytes " << bytes_write << std::endl;
// Step 7 the file size is double content.size()
memset((void*)&st_buf, 0x0, sizeof(struct stat));
ret = lstat(path.c_str(), &st_buf);
if (ret < 0) {
std::cerr << "lstat err path " << path
<< " errno " << errno << std::endl;
goto out;
}
std::cout << "lstat ok path " << path
<< " st_size " << st_buf.st_size << std::endl;
ret = 0;
out:
if (fd >= 0) {
close(fd);
}
return ret;
}
输出结果
open ok path ./test.txt fd 3
write ok fd 3 data hello_world nbytes 11
open again ok path ./test.txt fd 3
lseek ok SEEK_CUR fd 3 cur_off 0
read ok fd 3 data hello_world nbytes 11
lseek ok SEEK_SET fd 3 cur_off 5
append write ok fd 3 append data hello_world append nbytes 11
lstat ok path ./test.txt st_size 22
答案 2 :(得分:0)
O_APPEND标志强制文件指针仅指向文件末尾。所以如果你从文件的开头做lseek,它会将更新的文件指针位置作为文件的开头,即旧文件的结束位置。