Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

Reading formated date from a file C++

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 and year into 3 separate integers.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

vector<tm> 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...