0

So I've made a class for a location,in which I store it's coordonates and it's time,in utc. I overloaded the >> operator like this

friend ifstream& operator >>(ifstream& in, loc_list& l)
{
    char bf[40];
    in >> bf;
    l.setID(bf);
    long t=0;
    in >> l.utc;
    //l.setTime(t);
    double point;
    in >> point;
    l.p.setX(point);
    in >> point;
    l.p.setY(point);
    in >> l.speed;
    return in;

}

and the << operator like this:

friend ostream& operator <<(ostream& out,const loc_list &l)
{
    out << l.id << endl;
    out << put_time(gmtime(&l.utc),"%c %Z" )<< endl;
    //out << l.utc << endl;
    out << l.p.getX() << endl;
    out << l.p.getY() << endl;
    out << l.speed;
    return out;
}

however,the gmtime in the << operator works only when I create an object using the constructor. When I read it with cin>> it breaks. I've debugged the program but the objects contains the right data print from debugger

so,any thoughts?

1 Answers1

0

The counterpart of put_time is get_time.

I suggest using:

std::tm tm 
in >> get_time(&tm, "%c %Z");

and then update l.utc from tm.

l.utc = mktime(&tm);
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I can't read time like that because in my input file ,the time looks like this 1439467748512 – Ramona Mihaela Dinescu Jan 21 '16 at 15:29
  • @RamonaMihaelaDinescu, I tried testing the functionality in a simple program but the compiler I tried does not seem to support `std::put_time` or `std::get_time`. You can give it a try in your environment. Link: http://ideone.com/QqHoB2. – R Sahu Jan 21 '16 at 15:51