EmptyData EmptyData - 2 years ago 62
Linux Question

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) {
close( filedesc );

return 0;
return 0;


If i run same above second time the o/p is "unable to open file".
Am i doing something wrong?

Answer Source

This is a permission issue

Try changing




It will work fine


read, write, execute/search by others

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download