I am trying to make a program that copies the contents of any number of files into one big file. No breaks or anything needs to exist in between the contents of the files. I have a good working start, but it will only copy the contents of the first file I designate and not the rest. Also, at the end of the file, it puts this weird y looking character on the end. I am guessing my problem lies in the fgetc and fputc functions and probably the way I have the loop set up to copy and paste the characters as a whole. Anyways, I point in the right direction would be nice.
Code://the program basically copies the contents of files designated in the command line argument into one big file #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *concat, *fp; char c; int n; concat = fopen("concat.txt","w"); for(n=1; argv[n] != NULL; n++){ //loops through all files specified in command line argument if(fopen(argv[n], "r") == NULL){ //if the file cant be opened (aka NULL) then satte file cant be opened printf("Error. File ""%s"" cant be opened.\n", argv[n]); exit(0); } else{ //if the file can be opened, then open it in read mode fp=fopen(argv[n], "r"); while(c != EOF){ //while the file isn't at the end, EOF, copy each character into c then put c into concat c = fgetc (fp); fputc(c, concat); } fclose(fp); } } fclose(concat); system("PAUSE"); return 0; }



LinkBack URL
About LinkBacks


