我在 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/