2010-10-04 17 views
2

我已經花像一在這個例子中,每次我從這個代碼得到錯誤Unable to read cinios_base::iostate等於failbit方面的GET_TIME一直未能

#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); 
} 

一如既往,GCC 4.4.3在Ubuntu 10.4 x64

+0

你可以使用'ios_base :: goodbit'(或'ios :: goodbit')而不是'state == static_cast (0)'...儘管被命名爲「bit」,但它是一個用詞不當, 'goodbit'爲零。 – Potatoswatter 2010-10-04 18:11:25

+0

那麼...你通過'cin'給了什麼輸入? – Potatoswatter 2010-10-04 18:14:49

+0

輸入是2/2/2005 – Sambatyon 2010-10-04 18:21:52

回答

4

我在一個盒子上運行你的示例代碼,直到我輸入輸入02/02/2005,它就像你說的那樣失敗了。

它看起來像在月份和日期字段中的前導零是必要的。

+0

+1。根據C++標準提到的http://www.opengroup.org/onlinepubs/9699919799/functions/strptime.html,它們不應該是必需的。我猜GCC中存在問題。我無法報告,因爲我無法複製它;我在達爾文... – Potatoswatter 2010-10-04 19:41:44