Monday, June 18, 2012

To take the contents of a file and copy them into another file


/* Program that takes the contents of a file and copies them into another file */

#include<stdio.h>

#include<conio.h>

void main()

{

  FILE *fs,*ft;

  char ch,c;

  clrscr();

  fs=fopen("MYFILE","r");

  if(fs==NULL)

  {

    puts("Cannot open source file");

    exit(1);

  }

  ft=fopen("NEWFILE","w");

  if(ft==NULL)

  {

    puts("Cannot open target file");

    fclose(fs);

    exit(2);

  }

  while(1)

  {

    ch=fgetc(fs);

    if(ch==EOF)

      break;

    else

      fputc(ch,ft);

  }

  fclose(fs);

  fclose(ft);

  ft=fopen("NEWFILE","r");

  while((c=getc(ft))!=EOF)

    printf("%c",c);

  fclose(ft);

  getch();

}

 

OUTPUT

hello how are you.

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.