0
void mergeFile(){
    //allocate memory
    char * firFile = (char*) malloc(MAX_SIZE);
    char * secFile = (char*) malloc(MAX_SIZE);
    char * conFile = (char*) malloc(MAX_SIZE);
    char * buffer = (char*) malloc(MAX_SIZE);
    char ch;
    
    // get name of first file
    printf("Enter name of first file: ");
    __fpurge(stdin);
    gets(buffer);
    strcpy(firFile, FOLDER);
    strcat(firFile, buffer);
    
    //get name of second file
    printf("Enter name of second file: ");
    __fpurge(stdin);
    gets(buffer);
    strcpy(secFile, FOLDER);
    strcat(secFile, buffer);
    
    //get name of file will store
    printf("Enter name of file which will store contents of two files : ");
    __fpurge(stdin);
    gets(buffer);
    strcpy(conFile, FOLDER);
    strcat(conFile, buffer);

    //open 3 file with 'r' and 'w' mode
    FILE * firPtr = fopen(firFile,"r");
    FILE * secPtr = fopen(secFile, "r");
    FILE * conPtr = fopen(conFile, "w");
    
    //check 3 file NULL or not
    if (firPtr == NULL) {
        printf("Can not open %s file\n", firFile);
        remove(conFile);
    } else if (secPtr == NULL) {
        printf("Can not open %s file\n", secFile);
        remove(conFile);
    } else if (conPtr == NULL){
        printf("Can not open %s file\n",conFile);
    }else{
        // write all character in first file to file will store
        // MAY NOT WORK
        while ((ch = fgetc(firPtr)) != EOF)
            fprintf(conPtr, "%c", ch);
        
         // write all character in second file to file will store
         // MAY NOT WORK
        while ((ch = fgetc(secPtr)) != EOF)
            fprintf(conPtr, "%c", ch);
        printf("Two file were merged into %s file successfully\n!",conFile);
    }
    
    //clear all
    free(buffer);
    free(firFile);
    free(secFile);
    free(conFile);
    fclose(firPtr);
    fclose(secPtr);
    fclose(conPtr);
}

I use fget to get character from file and write to another file, I work well when i use two file, one for read and one for store, But when i try to merge two file to another file, this code didn't work, no things in side contains file. I run this code in Netbeans 8.2, can you give me mistake from this code, thanks so much!

Stack
  • 111
  • 6
  • 4
    `fgetc` returns `int`, but you are assigning the value to `ch` which is of type `char`. You should define `ch` as `int`, otherwise your `EOF` test will not work. This might then break your `fprintf` calls, but they are not necessary -- use `fputc` instead. You should probably open all these files in binary mode too -- add a `b` to the open mode in all calls to `fopen` (_i.e._ `"rb"`, `"wb"`) – paddy Jul 21 '20 at 05:30
  • 4
    Don't use `gets()` — ever! [It is far too dangerous to be used](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) and is no longer a part of Standard C (though it exists in most libraries for reasons of backwards compatibility). – Jonathan Leffler Jul 21 '20 at 05:51

0 Answers0