mkstemp関数を使うと他の名前がかぶらない一時ファイルのファイルディスクリプタが取得できます。
int mkstemp(char *template);
template
はXXXXXX
で終わる文字列です。
例
# include <stdio.h>
# include <stdlib.h>
int main() {
char template[] = "/tmp/testXXXXXX";
int fd = mkstemp(template);
FILE* fp = fdopen(fd, "w");
fprintf(fp, "Hello temporary file!\n");
fclose(fp);
return 0;
}
3回ほど実行したら次のファイルが作成されていました。
testbUU96t
testmJBD7R
testsjjNAU