您的位置:首页 > 编程语言 > C语言/C++

C语言写文件生成文件名为系统时间的方法

2018-03-08 23:41 399 查看
很多时候对于一些log文件需要记录生成时间,或者为了保证不同时间生成的文件名不一样就需要用系统时间来给文件命名。如下[cpp] view plain copy#include<stdlib.h>  
#include<time.h>  
#include<stdio.h>  
struct tm *newtime;  
          char outfile[128];  
          time_t t1;  
          t1 = time(NULL);   
          newtime=localtime(&t1);  
          strftime( outfile, 128, "data_%Y%m%d_%H%M%S.log", newtime);  
FILE *fp = fopen(outfile, "wt");  
特别注意上面strftime中的格式,大小写问题。反正我全部用大写时报错了,不明白还是按规定来吧转:http://blog.csdn.net/xinsuixiaofeiyu/article/details/35233221
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: