LINUX下建立临时文件: mkstemp
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
免费下载资源
·
LINUX下建立临时的方法(函数)有很多, mktemp, tmpfile等等. 今天只推荐最安全最好用的一种: mkstemp.
mkstemp (建立唯一临时文件)
头文件: #include <stdlib.h>
声明: int mkstemp(char * template)
返回值: 成功则返回0, 失败则返回-1 .
说明: 建立唯一临时文件名, template须以数组形式声明而非指针形式.
头文件: #include <stdlib.h>
声明: int mkstemp(char * template)
返回值: 成功则返回0, 失败则返回-1 .
说明: 建立唯一临时文件名, template须以数组形式声明而非指针形式.
template格式为: template.XXXXXX. 最后6位必须为XXXXXX, 前缀随意.
上面简单介绍了mkstemp的函数定义, 下面是我写的测试代码.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define LEN_FILE_NAME 256
#define TEMP_DIR "/home/daiweitao/Code/c/mkstemp/temp/"
#define AREA_LIST 'A'
#define GENRU_LIST 'B'
/* Make temparory file */
int mk_temp_file(char file[])
{
/* Return value */
int ret = 0 ;
/* Make temp file */
if (mkstemp(file) < 0 )
{
ret = -1 ;
}
else
{
printf( "Temp file name: [%s] " , file);
}
return ret;
}
/* Generate genru list file */
int genru_list()
{
/* Return value */
int ret = 0 ;
/* File pointer */
FILE *fp = NULL;
/* File name buffer */
char file[LEN_FILE_NAME];
/* Init */
memset(file, 0x00, sizeof (file));
/* Set initial value (template) */
strcpy(file, TEMP_DIR);
strcat(file, "cutman.genru.XXXXXX" );
/* TEMP */
if (mk_temp_file(file) < 0 )
{
perror("mkstemp" );
ret = -1 ;
}
else
{
if ((fp = fopen(file, "w")) == NULL)
{
perror("fopen" );
ret = -2 ;
}
}
fclose(fp);
return ret;
}
/* Generate area list file */
int area_list()
{
/* Return value */
int ret = 0 ;
/* File pointer */
FILE *fp = NULL;
/* File name buffer */
char file[LEN_FILE_NAME];
/* Init */
memset(file, 0x00, sizeof (file));
/* Set initial value (template) */
strcpy(file, TEMP_DIR);
strcat(file, "cutman.area.XXXXXX" );
/* TEMP */
if (mk_temp_file(file) < 0 )
{
perror("mkstemp" );
ret = -1 ;
}
else
{
if ((fp = fopen(file, "w")) == NULL)
{
perror("fopen" );
ret = -2 ;
}
}
fclose(fp);
return ret;
}
int cutman(char c)
{
/* Return value */
int ret = 0 ;
switch (c)
{
case AREA_LIST:
ret = area_list();
break ;
case GENRU_LIST:
ret = genru_list();
break ;
default :
printf("Operation type error " );
break ;
}
return ret;
}
int main()
{
return cutman(AREA_LIST);
}
#include <string.h>
#include <stdio.h>
#define LEN_FILE_NAME 256
#define TEMP_DIR "/home/daiweitao/Code/c/mkstemp/temp/"
#define AREA_LIST 'A'
#define GENRU_LIST 'B'
/* Make temparory file */
int mk_temp_file(char file[])
{
/* Return value */
int ret = 0 ;
/* Make temp file */
if (mkstemp(file) < 0 )
{
ret = -1 ;
}
else
{
printf( "Temp file name: [%s] " , file);
}
return ret;
}
/* Generate genru list file */
int genru_list()
{
/* Return value */
int ret = 0 ;
/* File pointer */
FILE *fp = NULL;
/* File name buffer */
char file[LEN_FILE_NAME];
/* Init */
memset(file, 0x00, sizeof (file));
/* Set initial value (template) */
strcpy(file, TEMP_DIR);
strcat(file, "cutman.genru.XXXXXX" );
/* TEMP */
if (mk_temp_file(file) < 0 )
{
perror("mkstemp" );
ret = -1 ;
}
else
{
if ((fp = fopen(file, "w")) == NULL)
{
perror("fopen" );
ret = -2 ;
}
}
fclose(fp);
return ret;
}
/* Generate area list file */
int area_list()
{
/* Return value */
int ret = 0 ;
/* File pointer */
FILE *fp = NULL;
/* File name buffer */
char file[LEN_FILE_NAME];
/* Init */
memset(file, 0x00, sizeof (file));
/* Set initial value (template) */
strcpy(file, TEMP_DIR);
strcat(file, "cutman.area.XXXXXX" );
/* TEMP */
if (mk_temp_file(file) < 0 )
{
perror("mkstemp" );
ret = -1 ;
}
else
{
if ((fp = fopen(file, "w")) == NULL)
{
perror("fopen" );
ret = -2 ;
}
}
fclose(fp);
return ret;
}
int cutman(char c)
{
/* Return value */
int ret = 0 ;
switch (c)
{
case AREA_LIST:
ret = area_list();
break ;
case GENRU_LIST:
ret = genru_list();
break ;
default :
printf("Operation type error " );
break ;
}
return ret;
}
int main()
{
return cutman(AREA_LIST);
}
执行结果:
$ ./a.out
Temp file name: [/home/daiweitao/Code/c/mkstemp/temp/cutman.area.bGbabc]
Temp file name: [/home/daiweitao/Code/c/mkstemp/temp/cutman.area.bGbabc]
GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
已为社区贡献6条内容
所有评论(0)