1

I have overloaded operator >> and I am trying to read a data from a file, i.e.

Store first;
std::wifstream in("file.txt");
in >> first;

Here is the code:

std::wistream & operator >> (std::wistream &is, Store &store)
{
    std::size_t vec_size; // size of employees_ vector
    std::getline(is, store.name_);
    is >> store.surface_area_;
    std::wcout << store.surface_area_ << std::endl;
    is >> vec_size;         
    std::wcout << vec_size << std::endl;
    ...
    return is;
}

name_ is of type wstring

surface_area_ is of type double

file.txt:

Euro AGD
1154,5
0
0

(I have set Polish $LANG, that's why there is comma instead of a dot)

And what I get is:

1 154,5
4 519 717 136

If I add is.ignore() right after getline I get:

154,5
0

My guess is that getline is causing a mess in a buffer. How to make this work?

SantaXL
  • 335
  • 2
  • 13
  • given that you use wifstream, is your text file stored in unicode? getline consumes endline character so maybe it messes up there – Oleg Bogdanov Jan 08 '17 at 00:24
  • Yes, text file is stored in unicode. Reading words with Polish characters like ą or ć works well. – SantaXL Jan 08 '17 at 00:28
  • 1
    Could it be that the space is the "thousands separator" in your locale? Difficult to believe, but it is showing like that... That might explain the first output, but not the second one though. I think there is something messing up with the locale settings vs file format. – A.S.H Jan 08 '17 at 00:47
  • @A.S.H Yes, when using Polish locale, space is a "thousands separator" and there is a comma instead of a dot. – SantaXL Jan 08 '17 at 00:52

1 Answers1

1

Ok, I think I have found the solution. The whole problem was that in a file there was 1154,5 instead of 1 154,5 - and everything works fine after this little change (without any ignore()). What is interesting, though, is a fact that 11 541,5 also works fine, but 111 541,5 is being rounded to 111 542 (whyyyyyy).

Another interesting question is, why the hack << store.surface_area_ saves a number in different format than >> is trying to read it. They REALLY should simplify encoding in C++, it is causing way too much of a trouble. EDIT: It does not, it was my bad after all.

SantaXL
  • 335
  • 2
  • 13
  • Good news. But I would never love to work with the space as thousands separator. That is going to be a real headache, Good luck! – A.S.H Jan 08 '17 at 01:05
  • 1
    Thanks for your suggestion with a thousands separator, I didn't notice the lack of a space before. We both learned something today :) Take care, mate – SantaXL Jan 08 '17 at 01:09