Program to Copy File into Another File

Write a program to copy the content of the file into Another File.
C Program
#include <stdio.h>

int main()
{

    FILE *fromFile, *toFile;
    char fromFileName[1000];
    char toFileName[1000];
    char ch;

    printf("Enter from file name with full path: ");
    scanf("%s"fromFileName);

    printf("Enter to file name with full path: ");
    scanf("%s"toFileName);

    fromFile = fopen(fromFileName"r");
    if (fromFile == NULL)
    {
        printf("Error in opening file \n");
        return -1;
    }
    toFile = fopen(toFileName"w");
    if (toFile != NULL)
    {
        printf("File created successfully!\n");
    }
    else
    {
        printf("Failed to create the file.\n");
        return -1;
    }
    // first file
    ch = getc(fromFile);
    while (ch != EOF)
    {

        putc(chtoFile);
        ch = getc(fromFile);
    }

    //close the files
    fclose(fromFile);
    fclose(toFile);

    return 0;
}

Input:

Enter from file name with full path: C:\Users\studentrecord.txt
Enter to file name with full path: C:\Users\hello.txt
Output:

File created successfully!

Input File:studentrecord.txt

Name = Rahul, marks = 67 Name = Suman, marks = 65 Name = Jeet, marks = 56 Name = Anuj, marks = 67
Output File: hello.txt

Name = Rahul, marks = 67 Name = Suman, marks = 65 Name = Jeet, marks = 56 Name = Anuj, marks = 67


No comments:

Post a Comment