2011-02-08 20 views
0

const char *をどのように分割できますか?split const char * in C++

私はconst charに保存された日付パターンを持っています。私はそれが有効かどうかを知りたいです。 私はconst char *を分割できないので、どうすればいいですか?

+0

'std :: string'はオプションですか? –

+0

質問は不明です。 const char *から日付を読み込みたいですか? – Aamir

+0

'const char *'を分割できないのはなぜですか?文字列を変更することはできませんが、文字列から部分文字列を抽出できるはずです。 – templatetypedef

答えて

2

sscanf()またはおそらくstrptime()を使用している場合は、文字バッファから日/月/年のフィールドを解析することができます。あなたは困難を持っている場合は、それらを試してみるとポスト具体的な質問すべき

std::istringstream is("2010/11/26"); 
int year, month, day; 
char c1, c2; 
if (is >> year >> c1 >> month >> c2 >> day && 
    c1 == '/' && c2 == '/') 
{ 
    // numeric date fields in year, month, day... 
    // sanity checks: e.g. is it really a valid date? 
    struct tm tm; 
    tm.tm_sec = tm.tm_min = tm.tm_hour = tm.tm_wday = tm.tm_yday = tm.tm_isdst = 0; 
    tm.tm_mday = day; 
    tm.tm_mon = month; 
    tm.tm_year = year; 
    time_t t = mktime(&tm); 
    struct tm* p_tm = localtime(&t); 
    if (p_tm->tm_mday == day && p->tm_mon == month && p->tm_year == year) 
     // survived to/from time_t, must be valid (and in range) 
     do something with the date... 
    else 
     handle date-like form but invalid numbers... 
} 
else 
    handle invalid parsing error... 

:あなたもたala、その後、数値変数に値をストリームのstd ::にstringstreamにテキストを置くことができます。

1

あなたの質問に詳細を追加する必要があります。一般に、boost::regex_matchを使用して、指定された正規表現が指定された文字シーケンスのすべてと一致するかどうかを判断できます。

関連する問題