You are on page 1of 4

//using I/O to copy one file to another file #include<fcntl.h> #include<stdio.h> #include<unistd.

h> int main(int c, const char * fn[]) { char ch; int fd1 = open(fn[1],O_RDONLY); int fd2 = open(fn[2],O_WRONLY); int count = 1; int a; while(count=read(fd1,&ch,1) != 0) { a = write(fd2,&ch,1); } if (a>0) { printf("Write success\n"); } else { printf("fail"); } return 0; }

//copy file to another file using I/O in reverce order #include<fcntl.h> #include<stdio.h> #include<unistd.h> int main(int c, const char * fn[]) { char ch; int fd1 = open(fn[1],O_RDONLY); int fd2 = open(fn[2],O_WRONLY); char st[BUFSIZ]="\0"; char copy[BUFSIZ]="\0"; int int int int int count = 1; a; i=0; j=0; k=0;

while(count=read(fd1,&ch,1) != 0) { st[i]=ch; i++; k++; } st[i]='\0'; printf("\nNumber of alphabet= "); printf("%d",i); while (i!= -1) { cpy[j++]=st[--i]; } a = write(fd2,&copy,k); if (a>0) { printf("\nreverse Data"); printf("\n"); printf("\n%s",copy); printf("\n"); } else { printf("fail"); } return 0; }

//using library to one file copy to another file in revers order #include<stdio.h> int main() { FILE *p,*q; char buff1[BUFSIZ],buff2[BUFSIZ]; //printf("buffer size: %d\n", sizeof(buff1)); p=fopen("/home/aimy/aiman/texta.txt","r"); setbuf(p,buff1); //printf("file cannot open/n"); q=fopen("/home/aimy/aiman/aa.txt","a"); setbuf(q,buff2); while(fgets(buff1,BUFSIZ,p)!=NULL) fputs(buff1,q); printf("\n %s",buff2); fclose(p); fclose(q); return 0; }

//copy one file to another file in reverse order using using library #include <stdio.h> #include <stdlib.h> #include<unistd.h> #include<sys/stat.h> int main() { FILE *p, *q; char buff1[BUFSIZ]; char buff2[BUFSIZ]; int a ; char c; struct stat statbuff; stat("/home/aimy/aiman/texta.txt", &statbuff); a=statbuff.st_size; p=fopen("/home/aimy/aiman/texta.txt","r"); q=fopen("/home/aimy/aiman/aa.txt","a"); setbuf(q,buff2); fseek(p,-1,SEEK_END); setbuf(p,buff1); while(a!=0){ c = fgetc(p); fputc(c,q); fseek(p,-2,SEEK_CUR); printf(" \n%c",c); a--; } fclose(p); fclose(q); return 0; }

You might also like