Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用fcntl.h在追加模式下写入文件_C++_Linux - Fatal编程技术网

C++ 如何使用fcntl.h在追加模式下写入文件

C++ 如何使用fcntl.h在追加模式下写入文件,c++,linux,C++,Linux,下面是我的代码: #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<<"unabl

下面是我的代码:

#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_IRWXO
由他人读取、写入、执行/搜索


参考

这是一个权限问题

试着改变

S_IRWXO 

它会很好用的

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


使用PrRor函数引用引用将提供额外的错误原因信息:有时混合C和C++,即C开路和C++ +CUT的混合。只考虑使用。@ RuulfMm Hulbouer-Oct不是C,它是POSIX。除非你有一个C++ POSIX包装器,否则我不知道他应该做什么,除非你想让他使用C++ STD的东西代替POSIX。使用PrRor函数会提供关于错误原因的附加信息:有时混合C++和C++,即C-OPEN和C++CUT的混合。只考虑使用。@ RuulfMm Hulbouer-Oct不是C,它是POSIX。除非你有一个C++ POSIX包装器,否则我不知道他应该做什么,除非你想让他使用C++ STD的东西而不是POSIX。