[Solved] Adding content of file 1 to file 2 in C [closed]


Open results[2] in append mode:

FILE *fp2;

fp2 = fopen(results[2], "a");  // a is for append

Then you can just loop through the first file and dump to the second one.. something like:

char line[100] = {0};
while (fgets(line,sizeof(line),fp) != NULL)
  fputs(line, fp2);

EDIT: Here’s a full compiling program that takes the contents of “test.txt” and appends it to “test2.txt”:

int main(int argc, char** argv) {
    FILE *fp;
    FILE *fp2;
    char line[100] = {0};
    char * results[2] = {"test.txt", "test2.txt"};
    fp = fopen(results[0], "r");
    fp2 = fopen(results[1], "a");  // a is for append

    while (fgets(line,sizeof(line),fp) != NULL)
       fputs(line, fp2);
    fclose(fp);
    fclose(fp2);
    return 0;
}

3

solved Adding content of file 1 to file 2 in C [closed]