Friday, June 20, 2014

//Exercise 2: Write a C Program to read the contents of file ‘File1’ and paste the contents at the beginning of another file ‘File2’ without taking help of any extra file.
#include<stdio.h>
void header()
{
   printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
   printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t  Lahore , Pakistan\n\n\n");
}

int main()
{
    FILE *a,*b;
    char f_1[100],f_2[100],ch;
    header();
    system("pause");
    system("cls");
    a=fopen("source.txt" , "w");
    if(a==NULL)
    {
        printf("\nOooppsss Unable to Open.....!!!");
        return 1;
    }
    printf("Write any text in File or Ctrl+Z to Exit the Program : ");
    gets(f_1);
    while(!feof(stdin))
    {
        fprintf(a,"%s",f_1);
        gets(f_1);
    }
    fclose(a);
    printf("\n\nEnter the name of Source File : ");
    gets(f_1);
    a=fopen(f_1 , "r");
    if (a==NULL)
    {
        printf("\nOooppsss .... Unable to Open the File....!!!");
        return 1;
    }
    printf("\n\nEnter the name of Target File in which you want to copy the Data :  ");
    gets(f_2);
    b=fopen(f_2,"w");
    if(b==NULL)
    {
        printf("\nOoooOOoopsss... Unable to Open the File....!!!");
        return 1;
    }
    while(!feof(a))
    {
        ch=fgetc(a);
        putc(ch,b);
    }
    fclose(a);
    fclose(b);
    printf("\n\n\t\tCongratulations....!!!!");
    printf("\n\n\t\tFile Has Been Copied Successfully...!!!\n\n");

}

No comments:

Post a Comment