Results 1 to 3 of 3

Thread: Merge two or more text files into one

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Post Merge two or more text files into one

    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:

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

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Merge two or more text files into one

    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
    Last edited by passel; Jun 16th, 2020 at 05:52 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: Merge two or more text files into one

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width