当我使用lcc
编译器并调用tmpnam(buf)
时,程序崩溃。lcc tmpnam crash
Reason: L_tmpnam indicates that buf must be 14 bytes long, while the string returned
is "D:\Documents and settings\Paul\Temporary\TmP9.tmp" which is much longer than 14.
我该怎么做,这种行为如何解释。
当我使用lcc
编译器并调用tmpnam(buf)
时,程序崩溃。lcc tmpnam crash
Reason: L_tmpnam indicates that buf must be 14 bytes long, while the string returned
is "D:\Documents and settings\Paul\Temporary\TmP9.tmp" which is much longer than 14.
我该怎么做,这种行为如何解释。
逐字从man tmpnam
:
不要使用此功能。改为使用mkstemp(3)或tmpfile(3)。
无论如何,你问它:
通过tmpnam()
所产生的名字由最大L_tmpnam
lenght 的文件名前缀的与名P_tmpdir
的目录。
所以缓冲区由传递给tmpnam()
最好声明(如C99):
char pathname[strlen(P_tmpdir) + 1 + L_tmpnam + 1] = ""; /* +1 for dir delimiting `/` and +1 for zero-termination */
如果非C99你可能会去为这个:
size_t sizeTmpName = strlen(P_tmpdir) + 1 + L_tmpnam + 1;
char * pathname = calloc(sizeTmpName, sizeof (*pathname));
if (NULL == pathname)
perror("calloc() for 'pathname'");
然后调用tmpnam()
这样的:
if (NULL == tmpnam(pathname))
fprintf(stderr, "tmpnam(): a unique name cannot be generated.\n");
else
printf("unique name: %s\n", pathname);
... /* do soemthing */
/* if on non C99 and calloc(() was called: */
free(pathname);
请显示一些代码... – 2013-04-07 09:07:50