gpt4 book ai didi

c++ - 如何使用 fcntl.h 以附加模式写入文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:30:20 27 4
gpt4 key购买 nike

下面是我的代码:

#include <iostream> 
#include <fcntl.h>
#include <unistd.h>
using namespace std;

int main()
{

int filedesc = open("testfile.txt", O_RDWR | (O_APPEND |O_CREAT) ,S_IRWXO);

if (filedesc < 0) {
cout<<"unable to open file";
return -1;
}

if (write(filedesc, "This will be output to testfile.txt", 36) != -1) {
cout<<"writing";
close( filedesc );
}

return 0;
return 0;

}

如果我在上面第二次运行相同的 o/p 是“无法打开文件”。我做错了什么吗?

最佳答案

这是权限问题

尝试改变

S_IRWXO 

 S_IRWXU

它会很好地工作

S_IRWXO

由他人读取、写入、执行/搜索

引用 http://pubs.opengroup.org/onlinepubs/7908799/xsh/sysstat.h.html

关于c++ - 如何使用 fcntl.h 以附加模式写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12741019/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com