Ç - 把当前日期的文件名 [英] C - putting current date in a filename

查看:85
本文介绍了Ç - 把当前日期的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个值: A,B,C,D 。做一个组计算这些值后,我希望我的code将结果输出形式的文件 ABCD_MM.DD.YY.txt ,保持轨道,当它做的。

我不是在C.做到这一点的最好办法很确定我使用有一个工作版本 itoa(),这是不是一个标准C函数和编译时会去(而且已经)无法识别的比我的其他机器。

这是code我必须这样做,可能有人用更好的(和普遍接受的)的方式帮助吗?该字符数组名称与全局范围定义。

 无效setFileName(){    现在time_t的;    结构TM *今天;
    焦炭日期[9];    //获取当前日期
    时间(安培,现在);
    今日=本地时间(安培,现在);    //打印在DD.MM.YY格式。
    的strftime(日期,15日,%d个%M%Y。今天);    字符的buff [20];
    焦瓦尔[20];    //放在一起格式的字符串:
    //\"ABCD_DD.MM.YY.txt
    的strcpy(瓦尔,itoa(A,浅黄色,20));
    strcat的(增值经销商,itoa(B,浅黄色,20));
    strcat的(增值经销商,itoa(C,BUFF,20));
    strcat的(增值经销商,itoa(D,浅黄色,20));    的strcpy(姓名,增值经销商);
    strcat的(名字,_);
    strcat的(姓名,日期);
    strcat的(姓名,名.txt);
}


解决方案

 字符文件名[FILENAME_MAX]
的snprintf(文件名,FILENAME_MAX,%D%D%D_%s.txt,A,B,C,D,日期);

I have 4 values: A,B,C,D. After doing a set of computations with those values, I want my code to output the results in a file of the form ABCD_MM.DD.YY.txt, to keep track of when it was done.

I'm not quite sure on the best way to do this in C. I have a "working" version using itoa(), which isn't a standard C function and will go (and has gone) unrecognized on machines other than mine when compiling.

This is the code I have for doing this, could someone help with a better (and universally accepted) way? The char array name was defined with a global scope.

void setFileName(){  

    time_t now;

    struct tm *today;  
    char date[9];

    //get current date  
    time(&now);  
    today = localtime(&now);

    //print it in DD.MM.YY format.
    strftime(date, 15, "%d.%m.%Y", today);

    char buff[20];
    char vars[20];

    //put together a string of the form:
    //"ABCD_DD.MM.YY.txt"
    strcpy(vars, itoa(A, buff, 20));
    strcat(vars, itoa(B, buff, 20));
    strcat(vars, itoa(C, buff, 20));
    strcat(vars, itoa(D, buff, 20));

    strcpy(name, vars);
    strcat(name, "_");
    strcat(name, date);
    strcat(name, ".txt");
}

解决方案

char filename [ FILENAME_MAX ];
snprintf(filename, FILENAME_MAX, "%d%d%d%d_%s.txt", A, B, C, D, date);

这篇关于Ç - 把当前日期的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆