Make a copy of file given in argv[1] to file given in argv[2]

How to write a C Program to Make a copy of file given in argv[1] to file given in argv[2] in C Programming Language ?

You have to use program with two arguments, the file names copy_from copy_to.

This C Program Make a copy of file given in argv[1] to file given in argv[2].

There is an access function with which we can see whether the file exists or not

 access(filename,F_OK);
 open 1. parameter= file name
 open 2. parameter for what we want to use the file
 O_RDONLY=only for reading,O_WRONLY-only for writing

There is errno variable in which there is the number of error --

 if (errno!=0) there is an error

The three parameter long version of open - it can create the file if it doesnt exist

there is a creat function as well - creat(filename,permissions);
O_TRUNC = if it existed, clear the content,
O_CREAT=create if it doesn't exist
3. parameter the permission, if it has to be created
S_IRUSR=permission for reading by the owner e.g.

Read gives back the number of bytes

    1. parameter the file descriptor
    2. parameter the address of variable, we read into
    3. parameter the number of bytes we want to read in

Write gives back the number of written bytes

      1. parameter the file descriptor
      2. parameter the address of variable we want to write out
      3. parameter the number of bytes we want to write out


Solution:

  1. //make a copy of file given in argv[1] to file given in argv[2]
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h> //open,creat
  5. #include <sys/types.h> //open
  6. #include <sys/stat.h>
  7. #include <errno.h> //perror, errno
  8. int main(int argc,char** argv){
  9.  if (argc!=3){perror("You have to use program with two arguments, the file names copy_from copy_to");exit(1);}
  10.  int f,g;
  11.  f=open(argv[1],O_RDONLY);
  12.  //there is an access function with which we can see whether the file exists or not
  13.  //access(filename,F_OK);
  14.  //open 1. parameter= file name
  15.  //open 2. parameter for what we want to use the file
  16.  //O_RDONLY=only for reading,O_WRONLY-only for writing
  17.  if (f<0){ perror("Error at opening the file\n");exit(1);}
  18.  //There is errno variable in which there is the number of error --
  19.  //if (errno!=0) there is an error
  20.  g=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
  21.  //the three parameter long version of open - it can create the file if it doesnt exist
  22.  //there is a creat function as well - creat(filename,permissions);
  23.  //O_TRUNC = if it existed, clear the content,
  24.  //O_CREAT=create if it doesn't exist
  25.  //3. parameter the permission, if it has to be created
  26.  //S_IRUSR=permission for reading by the owner e.g.
  27.  if (g<0){ perror("Error at opening the file\n");exit(1);}
  28.  char c;
  29.  while (read(f,&c,sizeof(c))){
  30.     //read gives back the number of bytes
  31.     //1. parameter the file descriptor
  32.     //2. parameter the address of variable, we read into
  33.     //3. parameter the number of bytes we want to read in
  34.     printf("%c",c); //we prints out the content of the file on the screen
  35.     if (write(g,&c,sizeof(c))!=sizeof(c))
  36.       {perror("There is a mistake in writing\n");exit(1);}
  37.       //write gives back the number of written bytes
  38.       //1. parameter the file descriptor
  39.       //2. parameter the address of variable we want to write out
  40.       //3. parameter the number of bytes we want to write out
  41.  }
  42.  close(f);
  43.  close(g);
  44.  return 0;
  45. }


Learn More :