Combine Files

I enjoy generating templates in C and am always combining files. This is a quick and easy way to do it that includes some basic error-handling. The required dependencies to use this method are stdio to enable working with files and stdlib in order to allow the use of exit.

                                void combine_file(FILE *dest_fp, FILE *source_fp){
	char ch;

	// check pointer for data
	if(dest_fp == NULL || source_fp == NULL){
		puts("ERROR: Could not open files");
        exit(0);
	}
	
	// read source character by character 
	// until end of file is reached
	while((ch = fgetc(source_fp)) != EOF ){
		// print each character in our destination file
		fputc(ch,dest_fp);
	}
}
                            

Here is an example of how to make use of the method.

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

void combine_file(FILE *dest_fp, FILE *source_fp);

int main(int argc, char *argv[]){
	
	FILE *destination_fp, *newContent_fp;
	
	// open a destination file in write mode
	destination_fp = fopen ("style.css", "w+");
	// here is our new content to append in read mode
	newContent_fp = fopen("header.css", "r");

	combine_file(destination_fp, newContent_fp);
	
	// remember to close our files
    fclose(newContent_fp);
    fclose(destination_fp);
	
	return 0;
}