Program to copy content of one file into another file

Program to copy content of one file into another file


Consider the following code in which we are reading content from a file and then writing that content into another file. This is called copying from one file to another:

CODE:

# include <stdio. h>

void main ()

{

    FILE *f1, *f2;

    char ch;

    int position;

    if ((f1 = fopen (“File1. txt”, “r”)) == NULL)

    {

        printf (“\n Unable  to open file”);

        return;

    }

    else

    {

        printf (“\n File opened\n “);

    }

    f2 = fopen (“File2. txt”, “w”);

    fseek (f1, 0L, END);

    position = ftell (f1);

    fseek (f1, 0L, SET);

    while (position –)

    {

        ch = fgetc (f1);

        fputc (ch, f2);

    }

    fcloseall ();

}

In the above example file1 is the file in which we have the data already written and from which we will copy the data and file2 is the file to which the data will be copied.