gpt4 book ai didi

c++ - 在 Linux 中打开文件。我不想创建写保护文件

转载 作者:太空狗 更新时间:2023-10-29 20:39:48 25 4
gpt4 key购买 nike

我在 Linux 中创建文件时遇到问题,它使我的文件处于写保护状态,我不知道为什么会这样。

void fileOperation::openFileWrite(char x, off_t s)
{
int fd;
char c[2] = {x};

fd = open("/home/stud/txtFile", O_CREAT | O_WRONLY); //open file
if(fd == -1)
cout << "can't open file" << endl;
else
{
lseek(fd, s, SEEK_SET);//seek at first byte
write(fd, (void*)&c, 2);//write to file
}
syncfs(fd);
::close(fd);
}

最佳答案

您必须使用额外的参数设置写权限(您的默认权限可能会取消写权限)

 fd = open("/home/stud/txtFile", O_CREAT | O_WRONLY, 0666);//open file

0666是一个八进制数,即每一个6对应三个权限位

6 = rw
7 = rwx

关于c++ - 在 Linux 中打开文件。我不想创建写保护文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26544214/

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