Program to Append the Content of File at the end of Another

Write a program to Append the Content of File at the end of Another.

C Program

#include <stdio.h>

int main()
{

    FILE *file1, *file2, *mergeFile;
    char fileName1[1000];
    char fileName2[1000];
    char mergeFileName[1000];

    char c;
    printf("Enter first file name with full path: ");
    scanf("%s"fileName1);

    printf("Enter second file name with full path: ");
    scanf("%s"fileName2);

    printf("Enter merge file name with full path: ");
    scanf("%s"mergeFileName);

    //open file in read mode
    file1 = fopen(fileName1"r");

    //open file in read mode
    file2 = fopen(fileName2"r");

    if (file1 == NULL || file2 == NULL)
    {

        printf("Error in opening file \n");
        return -1;
    }

    //open file in write mode
    mergeFile = fopen(mergeFileName"w");
    if (mergeFile != NULL)
    {
        printf("File created successfully!\n");
    }
    else
    {

        printf("Failed to create the file.\n");
        // exit status
        return -1;
    }

    // first file
    c = getc(file1);
    while (c != EOF)
    {
        putc(cmergeFile);
        c = getc(file1);
    }
    putc('\n'mergeFile);
    // second file
    c = getc(file2);
    while (c != EOF)
    {
        putc(cmergeFile);
        c = getc(file2);
    }

    //close files
    fclose(file1);
    fclose(file2);
    fclose(mergeFile);

    return 0;
}

Input:

Enter first file name with full path: C:\Users\studentrecord.txt
Enter second file name with full path: C:\Users\hello.txt
Enter merge file name with full path: C:\Users\merge.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

Input File: hello.txt

1 2 3 4

Output File: merge.txt
Name = Rahul, marks = 67 Name = Suman, marks = 65 Name = Jeet, marks = 56 Name = Anuj, marks = 67 1 2 3 4


No comments:

Post a Comment