Featured Post

Friday, July 29, 2016

UNIX program to copy one file contents to another file




/* unix program to copy one file contents to another file*/
/* copyfile.c*/

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>     

void main()
{
    int fd1,fd2;
    int n;

     char buf[1024];
     fd1=open("takr", O_RDONLY);     

/* opening an existing file named takr  in read only mode*/

     if(fd1==­1)
     {
        printf("error opening a file");
        exit(1);
     }

     fd2=open("myfile", O_CREAT|O_WRONLY, S_IRWXU);


      /*creating a new file named  myfile  in write only mode with permissions read
        write execute to owner*/
      /*whenever you are creating a new file you have to use O_CREAT along with
          mode  and also specify  permissions as a third parameter in open system call*/

     if(fd2==­1)
     {
        printf("\n error opening a file");
        exit(1);
    }
 while((n=read(fd1,buf,5))>0)  /* loops until n val bcoms zero, n can have range 0 to 5  */

  /* read system  call takes three parameters first one is file descriptor pointing to first file from where the data will be read and second one is buffer where the read data will be stored and third arguments specifies the maximum number of characters that can be read*/

   {
     write(fd2,buf,n);


/* same like read system call but now the data in the buffer will be written to second   
    file by using file descriptor fd2, third argument specifies the size of characters to 
     be written*/

   }
}


Output: /home/anil/~ cc copyfile.c
             /home/anil/~ ./a.out

/home/anil/~ cat takr

Hi how are you?

/home/anil/~ cat myfile


Hi how are you?

-------------------------------------------------------------------------------------------------

        http://cjuschools.blogspot.in/


copy,copyfile,open, write,creat,copy onf file content to another file, UNIX program to copy one file content to another file, UNIX program to copy one file to another file, C program to copy one file to another file in UNIX,read(), Modi banned 500rs and 1000rs. new 500rs notes, new 1000rs note, new 2000 rupee note, new currency with chip enabled, 2000rs note with chip enabled.logic behind banning,hange, tax exemption on money, tax exemption on toll gate free, ATMS not wroking , firday new notes released ,  why 500 and 1000rs notes banned  abdul kalam photo on 200rs n otes, tax on currency exc500 100rs notes clab sample programs RBI new rules regarding new currency notes,system call, write()system call, read() system call,write() system call, creat(), file handling system calls, counting number of characters in a file

No comments:

Post a Comment