简体   繁体   English

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

[英]How to write in file in append mode using fcntl.h

Below is my code: 下面是我的代码:

#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;

}

If i run same above second time the o/p is "unable to open file". 如果我第二次以上运行相同的o / p是“无法打开文件”。 Am i doing something wrong? 难道我做错了什么?

This is a permission issue 这是权限问题

Try changing 尝试改变

S_IRWXO 

to

 S_IRWXU

It will work fine 会很好的

S_IRWXO

read, write, execute/search by others 他人阅读,编写,执行/搜索

Reference http://pubs.opengroup.org/onlinepubs/7908799/xsh/sysstat.h.html 参考http://pubs.opengroup.org/onlinepubs/7908799/xsh/sysstat.h.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM