Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 使用sscanf()从字符串中指定月份名称和整数值?_String_Parsing_Date_Integer_Scanf - Fatal编程技术网

String 使用sscanf()从字符串中指定月份名称和整数值?

String 使用sscanf()从字符串中指定月份名称和整数值?,string,parsing,date,integer,scanf,String,Parsing,Date,Integer,Scanf,我有一个临时字符串: temp=“2014年1月16日01月12日59 OP grs0” 我想使用sscanf()和任何其他可能的方法从这个字符串中检索年、月、日期、小时、分钟和秒 我有一些伪代码,如: int ret_count = 0; char discard_msg_type[ENTRY_SIZE]=" "; /* message type is irrelevant */ *hour = *minute = *second = *month = *da

我有一个临时字符串: temp=“2014年1月16日01月12日59 OP grs0”

我想使用sscanf()和任何其他可能的方法从这个字符串中检索年、月、日期、小时、分钟和秒

我有一些伪代码,如:

     int ret_count = 0;

     char discard_msg_type[ENTRY_SIZE]=" ";  /* message type is irrelevant */

    *hour = *minute = *second = *month = *day = *year = -1 ;   /* All these are integer    pointers */ 

     ret_count = sscanf(temp,"%d %s %d %s %d %d %d",year,month,
                                day,discard_msg_type,
                                hour,minute,second);

执行后,我希望ret_count值为7,但结果为1。

格式字符串和数据之间存在类型不匹配问题:

Data:      "2014 Jan 16 01 12 59 OP grs0"
Format:    "%d   %s  %d %s %d %d %d"
格式字符串和参数之间也存在类型不匹配问题:

Format:    "%d   %s   %d   %s    %d   %d   %d"
Variables:  int* int* int* char* int* int* int*
您需要同时修复这两个问题。例如:

int yy, dd, hh, mm, ss;
char mmm[6];

if (sscanf(temp, "%d %s %d %d %d %d", &yy, mmm, &dd, &hh, &mm, &ss) != 6)
    ...report format error...
else if ((*month = map_month_name(mmm)) == -1)
    ...report unrecognized month name...
else
{
    *year   = yy;
    *day    = dd;
    *hour   = hh;
    *minute = mm;
    *second = ss;
}
其中
map\u month\u name()
可能是:

int map_month_name(char const *mmm)
{
    static char const *months[] =
    {
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    };
    for (size_t i = 0; i < sizeof(months)/sizeof(months[0]); i++)
    {
        if (strcmp(mmm, months[i]) == 0)
            return i + 1;
    }
    return -1;
}
int map\u month\u name(char const*mmm)
{
静态字符常量*months[]=
{
“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”
};
对于(大小i=0;i