我正在做以下转换和检查日期,但是,我不确定为什么以下日期保持验证为真.
不会%d只检查[01,31] + leading zeros?有没有更好,更准确的方法呢?
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main () {
struct tm tm;
char buffer [80];
char *str = "29/Jan/2012";
if (strptime (str, "%Y/%b/%d", &tm) == NULL)
exit(EXIT_FAILURE);
if (strftime (buffer,80,"%Y-%m-%d",&tm) == 0)
exit(EXIT_FAILURE);
printf("%s\n", buffer); // prints 29-01-20
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我知道正确使用freopen是为了省略分配,给出这篇文章:
freopen("/dev/tty","r", stdin);
我的问题是,我还应该检查返回值吗?我正在重新打开stdin并关闭它.例如:
if(freopen("/dev/tty","r", stdin)==NULL) {
fprintf(stderr, "Unable to redirect stdin from file\n");
exit(1);
}
Run Code Online (Sandbox Code Playgroud) 我必须迭代一个包含大约100M行的日志文件.我必须为多个日志执行此操作.平均线长为110个字符.
目前我正在循环查看可能的匹配列表.我想知道是否有更好的方法吗?
char *in_array(char *car) {
// longer list than this...
char *carlist[] =
{
"Avalon",
"Azera",
"Cayenne",
"Civic",
"Corolla",
"Elantra",
"F-150",
"Hilux",
"Lexus LS",
"Rav 4",
"Sienna",
// etc...
};
char *match;
int i;
int n = sizeof(carlist)/sizeof(carlist[0]);
for(i = 0; i < n; i++)
{
match = strstr(car, carlist[i]);
if(match != NULL)
{
return strdup(match);
}
}
return strdup("No match");
}
Run Code Online (Sandbox Code Playgroud) c ×3
arrays ×1
date ×1
freopen ×1
lookup ×1
performance ×1
strftime ×1
strptime ×1
validation ×1