Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15892

Merge two or more text files into one

$
0
0
Hi,

This is a small file merger to join many text files into a final output file.
Hope you find it usfull.

Code:

// File  : merge.c
// By    : Ben a.k.a DreamVB
// Date  : 21:15 16/06/2020
// Info  : Join two or more text files into one big final file.

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

int main(int argc, char *argv[])
{
    if(argc < 4){
        puts("Merge two or more files into a final output file.");
        printf("USE: %s Output.txt File1.txt File2.txt\n",argv[0]);
        exit(1);
    }

    //Open output file.
    FILE *fout = fopen(argv[1],"wb");

    //Check if output file was opened.
    if(fout == NULL){
        printf("Unable To Write Output File: %s",argv[1]);
        exit(2);
    }

    int x = 2;

    while(x < argc){
        //Open Input File.
        FILE *fp = fopen(argv[x],"rb");

        if(fp == NULL){
            printf("Unable To Read File: %s\n",argv[x]);
        }
        else{
            //While not end of file read chars
            while(!feof(fp)){
                //Get char from input file
                const char ch = fgetc(fp);
                //Check we are not at the end of the file.
                if(!feof(fp)){
                    //Write to output file.
                    fputc(ch,fout);
                }
            }
            //Close input file.
            fclose(fp);
        }
        //INC Counter
        x++;
    }

    fclose(fout);

    return 0;
}

How to use:

Quote:

merge output.txt File1.txt File2.txt File3.txt

Viewing all articles
Browse latest Browse all 15892

Trending Articles