Write a program to copy a text file to another file.


Input


#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fptr1, *fptr2;
    char filename[100], c;

    printf("Enter name of file to copy\n");
    scanf("%s", filename);

    fptr1 = fopen(filename, "r");
    if (fptr1 == NULL)
    {
        printf("Cannot open file %s \n", filename);
        exit(0);
    }

    printf("\nEnter name of target file\n");
    scanf("%s", filename);


    fptr2 = fopen(filename, "w");
    if (fptr2 == NULL)
    {
        printf("\nCannot open file %s \n", filename);
        exit(0);
    }

  
    c = fgetc(fptr1);
    while (c != EOF)
    {
        fputc(c, fptr2);
        c = fgetc(fptr1);
    }

    printf("\nFile copied successfully.\n");

    fclose(fptr1);
    fclose(fptr2);
    return 0;
}




Output


Enter name of file to copy
copyme

Enter name of target file
copyhere

File copied successfully.

No comments:

Post a Comment