# VBForums CodeBank > CodeBank - C++ >  Merge two or more text files into one

## BenJones

Hi,

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



```
// 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:*




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

----------


## passel

Of course, if you're at the command line anyway, I would just use copy

Copy File1.txt + File2.txt + File3.txt output.txt

If you wanted to copy all the txt files in a directory into one text file, you could simply do
Copy *.txt output.txt

----------


## BenJones

Thanks for the info passel will come in handy when I need to use it to copy files from folders.

----------

