11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C言語で一時ファイルの作成

Posted at

mkstemp関数を使うと他の名前がかぶらない一時ファイルのファイルディスクリプタが取得できます。

int mkstemp(char *template);

templateXXXXXXで終わる文字列です。

# 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
11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?