program to copy the content of one file into another file-by enggfact - Enggfact

Latest

Convert your Passion into your Profession

Wednesday, December 4, 2019

program to copy the content of one file into another file-by enggfact

Hello geeks,

Today we are going to learn the program to copy the content of one file to another file.For copying the content of the required file you must create that particular file in .txt format means in word file format.

For creating your file in .txt format firstly you have to go in your particular folder where you save your all programs then right click through your mouse,there you are able to see an option of creating new file.For creating new file in .txt format click the new file option.Through this you are able to create it.Now name your file whatever you want to create but don't write .txt after the name of the file because your computer is enough smart that it knows how to give it an extension so leave it.Now you are able to see your file which you want to create now write your content in the file.

As your file is created so follow our program to copy it in another file.

Before going to create a program I want to explain one thing to you is that you should not create the another file(copied file where you want to copy the content of original file by yourself). Because this program automatically create that copied file by itself.

Now I think you have understand it properly so we can start our program.

#include<studio.h>
#include<process.h>
#include<windows.h>//because we are using sleep function here so we have to use windows.h header file.
main()
{
  FILE *fp1,*fp2;
  char ch;
  fp1=fopen("record.txt","r");//here in place of record.txt use your file name like cprogram.txt and here 'r' represents file opened in read mode only.
  fp2=fopen("copy.txt","r");
  if(fp1==NULL||fp2==NULL)
  {
    printf("\t***some error occurred***\t");
    printf("unable to continue");
    exit(1);
   }
while((ch=fgetc(fp1))!=EOF)// EOF represents end of file.
  {
    fputc(ch,fp2);
  }
 printf("file copied successfully\n\n");
 fclose(fp1);//fclose is used to close the open file.
 fclose(fp2);
 printf("content of the original file is...\n\n");
 fp2=fopen("copy.txt","r");
 while((ch=fgetc(fp2))!=EOF)
 {
  printf("%c",ch);
  sleep(250);//to show characterwise printing of the content of the file of the original file.
 }
 fclose(fp2);

Now this program is successfully created so you can make it yourself and check it in your compiler.
If you like our stuffs for programming then please smash the follow button and follow us on other social sites for any query related to the particular stuffs.

Thankyou geeks!
Have a nice learning!

No comments:

Post a Comment