Reading formated date from a file C++

后端 未结 2 1226
南方客
南方客 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:28

    You could try something like this:

    // 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";
    }
    

    The above code could be placed in a (while) loop reading the file line by line.

提交回复
热议问题