C语言中在Linux下建立临时文件编译不通过,该怎么处理
C语言中在Linux下建立临时文件编译不通过
#include <stdio.h>
#include <stdlib.h> /*包含头文件。*/
main()
{
extern int errno; /*设置一个错误号。*/
char path[]="mytemp-XXXXXX"; /*定义文件名。*/
if(mkstemp(path)!=-1) /*建立一个临时文件。*/
{
printf("created temp file %s.\n",path); /*显示文件已经建立。*/
}
else /*另一种情况。*/
{
printf("cant't create temp file %s.\n",path); /*显示不能创建临时文件。*/
printf("errno:%d\n",errno); /*显示错误号。*/
printf("ERR :%s\n",strerror(errno)); /*显示错误信息。*/
}
}
运行环境ubuntu11.04自代GCC , 编译不通过。
------解决方案--------------------
#include <string.h>
#include <errno.h>
#include <error.h>
请包涵上门的头文件即可。
------解决方案--------------------
#include <stdio.h>
#include <stdlib.h> /*包含头文件。*/
main()
{
extern int errno; /*设置一个错误号。*/
char path[]="mytemp-XXXXXX"; /*定义文件名。*/
if(mkstemp(path)!=-1) /*建立一个临时文件。*/
{
printf("created temp file %s.\n",path); /*显示文件已经建立。*/
}
else /*另一种情况。*/
{
printf("cant't create temp file %s.\n",path); /*显示不能创建临时文件。*/
printf("errno:%d\n",errno); /*显示错误号。*/
printf("ERR :%s\n",strerror(errno)); /*显示错误信息。*/
}
}
运行环境ubuntu11.04自代GCC , 编译不通过。
------解决方案--------------------
#include <string.h>
#include <errno.h>
#include <error.h>
请包涵上门的头文件即可。
------解决方案--------------------
- C/C++ code
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> int main(int argc, char* const argv[]) { char template[] = "mytemp-XXXXXX"; int fd = mkstemp(template); if (fd == -1) { perror("mkstemp:"); return -1; } close(fd); return 0; }