Tuesday 28 February 2012

File Copy programs in C language

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 
void main(int arg,char *arr[])
{
    FILE *fs,*ft;
    char ch;
    clrscr();
    if(arg!=3)
    {
    printf("Argument Missing ! Press key to exit.");
    getch();
    exit(0);
    }
 
    fs = fopen(arr[1],"r");
    if(fs==NULL)
    {
    printf("Cannot open source file ! Press key to exit.");
    getch();
    exit(0);
    }
 
    ft = fopen(arr[2],"w");
    if(ft==NULL)
    {
    printf("Cannot copy file ! Press key to exit.");
    fclose(fs);
    getch();
    exit(0);
    }
 
    while(1)
    {
    ch = getc(fs);
    if(ch==EOF)
    {
    break;
    }
    else
    putc(ch,ft);
    }
 
    printf("File copied succesfully!");
    fclose(fs);
    fclose(ft);
}

No comments:

Post a Comment