Reading formated date from a file C++

后端 未结 2 1225
南方客
南方客 2020-12-12 06:54

So I have this file with multiple dates like this:

2.10.2015
13.12.2016
...

I\'m wondering how to read from this file and store day, month

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 07:29

    Given an istream foo which contains the dates you'll want to use get_time:

    vector bar;
    tm i;
    
    while(foo >> get_time(&i, "%d.%m.%Y")) bar.push_back(i);
    

    Live Example

    Of course defensive input is best practice, and doing that can be very challenging for a complex input type like a date. If you're going for that you might find this helpful: https://stackoverflow.com/a/29413535/2642059

提交回复
热议问题