Write a program to copy one file multiple times with different names (make multiple duplicate files).
#include<stdio.h> #include<string.h> #include<stdlib.h> int main(int argc,char **argv) { char file[32],ch; FILE *fp,**ptr; int i; if(argc < 3) { printf("Usage: ./a.out filename filename\n"); return; } fp = fopen(argv[1],"r"); if(fp == NULL) { printf("file is not present in this directory\n"); return 0; } ptr = (FILE**)malloc(sizeof(FILE*)*(argc-2)); for(i = 0;i < (argc-2);i++) { ptr[i] = (FILE*)malloc(sizeof(FILE)); ptr[i] = fopen(argv[i+2],"w"); while((ch = fgetc(fp)) != EOF) { fputc(ch,ptr[i]); } rewind(fp); fclose(ptr[i]); } printf("Copying Done....\n"); return 0; }
Note: File name data is present in the current working directory.
cat data Lot To Learn BlackBerry Iphone Incredible India
Output
./a.out Usage: ./a.out filename filename
./a.out data d1 d2 d3 Copying Done....
cat d1 Lot To Learn BlackBerry Iphone Incredible India
cat d2 Lot To Learn BlackBerry Iphone Incredible India
cat d3 Lot To Learn BlackBerry Iphone Incredible India