I need to read only one file and write only another file.

I tried to write some code, but there is a lot of errors.

There is my come:

#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 f_write(const char *name, int oflag, mode_t mode);


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

int f_write(const char *name, int oflag, mode_t mode){
   int dskr;
   dskr = write( name, O_WRONLY | O_CREAT | O_TRUNC, 0644 );
   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] );
   f = f_write( argv[2] );
   return 0;
}

Everything I got is errors:

edgals_rw01.c: In function ‘f_write’:
edgals_rw01.c:29:4: warning: passing argument 1 of ‘write’ makes integer from pointer without a cast
/usr/include/unistd.h:532:16: note: expected ‘int’ but argument is of type ‘const char *’
edgals_rw01.c:29:4: warning: passing argument 2 of ‘write’ makes pointer from integer without a cast
/usr/include/unistd.h:532:16: note: expected ‘const void *’ but argument is of type ‘int’
edgals_rw01.c: In function ‘main’:
edgals_rw01.c:54:4: error: too few arguments to function ‘f_write’
edgals_rw01.c:27:5: note: declared here

1 Answers

0
einpoklum On

Just listen to your compiler:

  • The write() system call takes a file descriptor (which has an int type) - not a filename. But you're trying to call it with name.
  • Your f_write() function takes two parameters which it isn't using - flags and open mode; you are actually setting those to fixed values - so just drop those parameters.