-
Jun 16th, 2020, 03:20 PM
#1
Thread Starter
Fanatic Member
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
-
Jun 16th, 2020, 05:47 PM
#2
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
-
Jun 17th, 2020, 04:24 AM
#3
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|