- 追加された行はこのように表示されます。
- 削除された行は
このように表示されます。
!!!C 一時ファイル
::tmpnam
[Programming 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
!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);
}
}
----
この本からの覚書。
{{amazon 4797327014}}