C open() with O_TRUNC| O_WRONLY| O_CREAT doesn’t let me reuse the file the second time

  c++, file-handling, linux

The permissions and mode as follows O_TRUNC| O_WRONLY| O_CREAT, S_IRUSR|S_IWUSR
don’t let me reuse the file at the second run for some reason.

Why is that? I have tried using 0666 for the permissions as well.

My source code snippet is –

int file_desc2 = open("./f-processed", O_TRUNC| O_WRONLY| O_CREAT, S_IRUSR|S_IWUSR);
    if( file_desc2 == -1){
        printf("FILE2 COULDNT BE OPENEDn");
        close(file_desc1);
        close(file_desc2);
        return -1;
    }

 
// WRITING SOME STUFF TO THE FILE HERE (works fine and can actually inspect the content manually

close(file_desc2);

Source: Windows Questions C++

LEAVE A COMMENT