C Program To Merge Two Files


♣ Try it Out ::
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

main()
{
    FILE *fs1, *fs2, *ft;

    char ch, file1[20], file2[20], file3[20];

    printf("Enter name of first file: ");
    gets(file1);

    printf("Enter name of second file: ");
    gets(file2);

    printf("File name which will store contents of two files: ");
    gets(file3);

    fs1 = fopen(file1, "r");
    fs2 = fopen(file2, "r");

    if(fs1 == NULL || fs2 == NULL)
    {
        perror("Error ");
        getch();
        exit(EXIT_FAILURE);
    }

    ft = fopen(file3, "w");

    if(ft == NULL)
    {
        perror("Error ");
        exit(EXIT_FAILURE);
    }

    while((ch=fgetc(fs1)) != EOF)
        fputc(ch, ft);

    while((ch=fgetc(fs2)) != EOF)
        fputc(ch, ft);

    printf("Two files were merged into %s file successfully. \n", file3);

    fclose(fs1);
    fclose(fs2);
    fclose(ft);

    getch();
    return(0);
}

♣ Output ::
Enter name of first file: file1.txt
Enter name of second file: file2.txt
File name which will store contents of two files: file3.txt
Two files were merged into file3.txt file successfully.

♣ Downloads ::

Comments

Popular posts from this blog

C++ :: Topological Sort Algorithm (using DFS)

How to Hack Facebook Account

C++ :: Strongly Connected Components Algorithm (SCC)