gpt4 book ai didi

C:将使用 tmpfile() 函数创建的临时文件与多线程合并

转载 作者:行者123 更新时间:2023-11-30 16:03:30 25 4
gpt4 key购买 nike

我有 n 个文件,我的程序需要使用线程和临时文件将所有文件的内容合并到一个文件中(必须使用 tmpfile())。创建线程时,它必须将 2 个文件合并到临时文件 (temp1) 中,然后另一个线程将接下来的 2 个文件合并到另一个临时文件 (temp2) 中,依此类推,然后在下一个级别中,另一个线程应该将 temp1 与 temp2 合并到另一个临时文件。

alt text

我正在考虑创建一个文件名数组,将其作为参数传递给 pthread_create ,并且该函数也应该返回修改后的数组,但我不知道如何获取临时文件名。将会是这样的:

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?
}

问题是:如何访问之前在上一个线程中使用 tmp() 打开的临时文件来生成新的临时文件?

最佳答案

您能否使用tmpnam()而不是tmpfile(),并根据需要手动打开/关闭/删除文件?

关于C:将使用 tmpfile() 函数创建的临时文件与多线程合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4069825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com