facet的get_time一直都在失败

时间:2010-10-04 18:05:20

标签: c++ g++ locale

我在此示例中花了一个时间,每次我从此代码中获得Unable to read cin ios_base::iostate等于failbit的错误:

#include "dates.h"

#include <iostream>
#include <ctime>
#include <locale>
#include <sstream>
#include <iterator>

using namespace std;

void trasnlateDate(istream&in, ostream&out){
    const time_get<char>& dateReader = use_facet<time_get<char> >(in.getloc());
    ios_base::iostate state = static_cast<ios_base::iostate>(0);
    istreambuf_iterator<char> end;

    tm t;

    dateReader.get_date(in, end, in, state, &t);

    if(state == static_cast<ios_base::iostate>(0) || state == ios_base::eofbit){
        const time_put<char>& dateWriter = use_facet<time_put<char> >(out.getloc());
        char fmt[] = "%x";
        if(dateWriter.put(out, out, out.fill(), &t, &fmt[0], &fmt[2]).failed())
            cerr << "unable to write to output stream.\n";
    }else{
        cerr << "Unable to read cin.\n";
    }
}

int main(){
    locale::global(locale(""));
    cin.imbue(locale("en_US.utf8"));
    cout.imbue(locale("de_DE.utf8"));
    trasnlateDate(cin, cout);
}

一如既往,Ubuntu 10.4 x64中的gcc 4.4.3

1 个答案:

答案 0 :(得分:4)

我在一个盒子上运行了你的示例代码,直到我输入02/02/2005的输入,它就像你说的那样失败了。

看起来像月和日字段中的前导零是必要的。