tags:

views:

30

answers:

1

I have spend like one out in this example, and everytime I get the error Unable to read cin with an ios_base::iostate equal to failbit from this code:

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

as always, gcc 4.4.3 in Ubuntu 10.4 x64

+2  A: 

I ran your sample code on a box, and until I entered the input 02/02/2005, it failed just like you said.

It looks like those leading zeros in month and day fields are necessary.

Dysaster
+1. According to http://www.opengroup.org/onlinepubs/9699919799/functions/strptime.html, to which the C++ Standard refers, they should not be necessary. Bug in GCC I guess. I can't report it because I can't reproduce it; I'm on Darwin…
Potatoswatter