「C 一時ファイル」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==C 一時ファイル== [Programming C] ====tmpnam==== 一意のファイル名を作成 #include <stdio.h> char *tmpnam(char *s); #include <stdio.h> #inc…」) |
|||
| 1行目: | 1行目: | ||
==C 一時ファイル== | ==C 一時ファイル== | ||
| − | [Programming C] | + | [[Programming C]] |
====tmpnam==== | ====tmpnam==== | ||
一意のファイル名を作成 | 一意のファイル名を作成 | ||
| − | #include | + | #include <stdio.h> |
char *tmpnam(char *s); | char *tmpnam(char *s); | ||
| − | #include | + | #include <stdio.h> |
| − | #include | + | #include <stdlib.h> |
| − | #include | + | #include <string.h> |
int main() { | int main() { | ||
| 24行目: | 24行目: | ||
一時fileの作成とopenを同時に行う | 一時fileの作成とopenを同時に行う | ||
fileは読み書き用にopenされ、fileへのすべての参照がcloseされた時点で自動的に削除される。 | fileは読み書き用にopenされ、fileへのすべての参照がcloseされた時点で自動的に削除される。 | ||
| − | #include | + | #include <stdio.h> |
FILE *tmpfile(void); | FILE *tmpfile(void); | ||
| − | #include | + | #include <stdio.h> |
| − | #include | + | #include <stdlib.h> |
int main() | int main() | ||
2020年2月15日 (土) 08:01時点における版
C 一時ファイル
tmpnam
一意のファイル名を作成
#include <stdio.h> char *tmpnam(char *s);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char path[255];
char *tmppath;
strcpy(path, "/var");
tmppath = tmpnam(path);
printf("%s\n", tmppath);
exit(0);
}
tmpfile
一時fileの作成とopenを同時に行う fileは読み書き用にopenされ、fileへのすべての参照がcloseされた時点で自動的に削除される。
#include <stdio.h> FILE *tmpfile(void);
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c;
FILE *tmp;
tmp = tmpfile();
if (tmp) {
printf("open temp file OK.\n");
} else {
exit(1);
}
while( (c = fgetc(stdin)) != 'x' ) {
fputc(c, tmp);
fflush(tmp);
}
fseek(tmp, 0, SEEK_SET);
while( (c = fgetc(tmp)) != EOF ) {
fputc(c, stdout);
}
}
この本からの覚書。
© 2006 矢木浩人