Q: C: merging temp files created with tmpfile() function with multithreading I have n files and my program needs to merge the contents of all into an one file using threads and temp files (have to use tmpfile()). When creating a thread it has to merge 2 files into a temp file (temp1) then another thread will merge the next 2 files into another temp file (temp2) and so on, then in the next level another thread should merge temp1 with temp2 into another temp file.

I was thinking in creating an array of file names passing it to pthread_create as argument, and the function should return the array modified too but i can't figure how to get the tempfile name. Will be something like this:
int main(int argc, char *argv[]){

int n = argc -1;

char *files_arr[n];

pthread_t threads[n-1];

}

    for (int i=0; i < argc; i++)

    {
       pthread_create (&threads[i], NULL, temp_merge, (void *) &files_arr);
    }
}//end main

void *temp_merge (void *arg){

    char *myarray[];
    myarray = (char *) arg;
    FILE *f1, *f2, *tf;
    tf = tmpfile();

    //code to merge f1 and f2 into tf, f1 and f2 could be temp files created before

     pthread_exit((void*) myarray); //Do I lose the temp file using pthread_exit? 
}

The question is: How can I access to a temp file openned before with tmp() in a previous thread to generate new temporary files? 

A: Could you use tmpnam() instead of tmpfile(), and manually open/close/delete files as necessary?

A: You have the list of files to begin with (I assume), so you'll open all of them (returning FILE *).
In your mergesort function (MSF), tmpfile() returns a FILE * which is what your MSF should return.  So, your return value from the MSF becomes one of the two inputs your next invocation of MSF.  This continues until you have only one FILE * to act upon.
