Wednesday, 20 February 2013

Program:To write a c program that makes a copy of a file using system calls

/*
 Program:To write a c program that makes a copy of a file using system calls       
        
*/ 
 #include
      #include
   #include
   #include
   main(int argc,char *argv[])
   {
           char ch;
           int fd1,fd2;
           fd1=open(argv[1],O_RDONLY);
          if(fd1<0 br="">          {
                  printf("\n File opening problem\n");
                  return;
          }
          fd2=open(argv[2],O_CREAT | O_WRONLY);
          if(fd2<0 br="">          {
                  printf("\n File openinng problem\n");
                  return;
          }
          while(1)
          {
                  if(read(fd1,&ch,1)<=0)
                          break;
                  write(fd2,&ch,1);
          }
          close(fd1);
          close(fd2);
          printf("\n File copied successfully\n");
  }


/*-----------------------INPUT/OUTPUT--------------

-bash-3.2$ cc mycp.c -o mycp
-bash-3.2$ ./mycp hello.c cp.c

 File copied successfully


--------------------------------------------------*/                                                 

No comments:

Post a Comment