linux C创建文件夹然后创建txt文件添加内容
Posted 非是非非
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux C创建文件夹然后创建txt文件添加内容相关的知识,希望对你有一定的参考价值。
void StoreEvent(char *str)
{
long tt;
struct tm *vtm;
int i;
char Etime[100];
char filename[20];
char dirname[20];
char path[100];
getcwd(path, sizeof(path));
time( &tt );
vtm = localtime( &tt );
sprintf(Etime,"%d-%02d-%02d %02d:%02d:%02d ",vtm->tm_year+1900,vtm->tm_mon+1,vtm->tm_mday,vtm->tm_hour,vtm->tm_min,vtm->tm_sec);
sprintf(filename,"/%d%02d%02d.txt",vtm->tm_year+1900,vtm->tm_mon+1,vtm->tm_mday);
sprintf(dirname,"/%d%02d",vtm->tm_year+1900,vtm->tm_mon+1);
strcat(path,dirname);
if(access(path,0)==-1) //access函数是查看文件是不是存在
{
if (mkdir(path,0777)) //如果不存在就用mkdir函数来创建
{
printf("creat file bag failed!!! ");
}
}
{
long tt;
struct tm *vtm;
int i;
char Etime[100];
char filename[20];
char dirname[20];
char path[100];
getcwd(path, sizeof(path));
time( &tt );
vtm = localtime( &tt );
sprintf(Etime,"%d-%02d-%02d %02d:%02d:%02d ",vtm->tm_year+1900,vtm->tm_mon+1,vtm->tm_mday,vtm->tm_hour,vtm->tm_min,vtm->tm_sec);
sprintf(filename,"/%d%02d%02d.txt",vtm->tm_year+1900,vtm->tm_mon+1,vtm->tm_mday);
sprintf(dirname,"/%d%02d",vtm->tm_year+1900,vtm->tm_mon+1);
strcat(path,dirname);
if(access(path,0)==-1) //access函数是查看文件是不是存在
{
if (mkdir(path,0777)) //如果不存在就用mkdir函数来创建
{
printf("creat file bag failed!!! ");
}
}
strcat(path,filename);
FILE *fp;
fp = fopen(path,"ar+");
if(fp == NULL)
{
printf("文件异常。 ");
return;
}
lseek(fp,0,SEEK_END); //将读写位置移到文件尾
strcat(Etime,str);
fputs(Etime,fp);
fflush(fp);
fsync(fileno(fp)); //同步缓存
fclose(fp);
}
FILE *fp;
fp = fopen(path,"ar+");
if(fp == NULL)
{
printf("文件异常。 ");
return;
}
lseek(fp,0,SEEK_END); //将读写位置移到文件尾
strcat(Etime,str);
fputs(Etime,fp);
fflush(fp);
fsync(fileno(fp)); //同步缓存
fclose(fp);
}
以上是关于linux C创建文件夹然后创建txt文件添加内容的主要内容,如果未能解决你的问题,请参考以下文章