从文件C ++读取格式化的日期 [英] Reading formated date from a file C++
本文介绍了从文件C ++读取格式化的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有这个文件,其中包含多个日期,如下所示:
So I have this file with multiple dates like this:
2.10.2015
13.12.2016
...
我想知道如何从此文件读取并存储日,月和年份分为3个独立的整数。
I'm wondering how to read from this file and store day, month and year into 3 separate integers.
谢谢。
推荐答案
尝试这样的事情:
// construct stream object and open file
std::ifstream ifs(file_name.c_str());
// check if opened successfully
if (!ifs) std::cerr <<"Can't open input file!\n";
int year, month, day;
char dot;
// extract date
ifs >> day >> dot >> month >> dot >> year;
// check input format
if (dot != '.') // add ranges for month and days validity
{
std::cerr <<"Wrong date format!\n";
}
以上代码可以放在( while
)循环逐行读取文件。
The above code could be placed in a (while
) loop reading the file line by line.
这篇关于从文件C ++读取格式化的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文