I need using open() function to creat file if it not exist, but if so, then to clear that file data.

There is my code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int f_open(const char *name, int oflag, mode_t mode);

int f_open(const char *name, int oflag, mode_t mode){
   int dskr;
   dskr = open( name, O_WRONLY );
   if( dskr == -1 ){
      perror( name );
      exit(1);
   }
   printf( "dskr = %d\n", dskr );
   return dskr;
}

int main( int argc, char *argv[] ){
   int d;
   int f;
   if( argc != 2 ){
      printf( "Naudojimas:\n %s failas_ar_katalogas\n", argv[0] );
      exit( 255 );
   }
   d = f_open( argv[1], O_CREAT | O_TRUNC, 0644 );
   return 0;
}

When I execute it and wrote a not existing file, then the output is no such a file or directory, but I need to creat that file. Or if I wrote a existing file with some text, then that data didn't cleared.

0 Answers