C++ C+中截至日期的字符串+;[使用Anjuta IDE]

C++ C+中截至日期的字符串+;[使用Anjuta IDE],c++,date,anjuta,C++,Date,Anjuta,我正在写一个程序,我需要从文件中读取日期。日期是年、月和日。我需要如何阅读所有日期信息??你能举一些例子吗?我建议使用。我不知道你想要什么样的内部格式,但这应该适合你。注意,它不做任何错误检查 struct tm tm; time_t t; strptime("%Y:%m:%d", &tm); printf("year: %d; month: %d; day: %d;\n", tm.tm_year, tm.tm_mon, tm.tm_mday); t = mktime(&

我正在写一个程序,我需要从文件中读取日期。日期是年、月和日。我需要如何阅读所有日期信息??你能举一些例子吗?

我建议使用。我不知道你想要什么样的内部格式,但这应该适合你。注意,它不做任何错误检查

struct tm tm;
time_t t;
strptime("%Y:%m:%d", &tm);
printf("year: %d; month: %d; day: %d;\n",
    tm.tm_year, tm.tm_mon, tm.tm_mday);
t = mktime(&tm);

您还可以使用“.”字符拆分字符串,并将数据放入变量中(例如,可以是数组)。

然后您可以通过组合它们来创建自己的格式和字符串。

首先,您可能需要一个结构来保存这些值。有一个标准的struct,tm,但是这个struct有很多成员,其中一些成员依赖于其他成员,当yday与wday和mday不一致时,就会产生混乱

struct Date {
    int year;
    int month;
    int day;
};
然后需要一个能够将数据读入结构的函数。您首先需要打开文件,读取第一行,然后处理它。为了实现这一点,可以使用IFStand,它是C++中的标准类来读取文件。
std::ifstream f( fileName.c_str() );
然后,您需要读取一行,其中存储了日期。因为这是一个练习,我想这是第一个
getline()
从输入中读取整行,并将其存储在先前创建的字符串中

std::string line;
std::getline( f, line );
最后,您必须处理该行。有多种方法来实现这一点,但是C++中最舒服的一种方法是使用与字符串相关的流,并按其类型读取每个字段。
 std::istringstream str( line );

 str >> date.year
     >> firstDot
     >> date.month
     >> lastDot
     >> date.day
;
关于错误检查,您可以进行各种验证(我将留给您)。至少,我们应该检查我们是否已经在应该读取的地方将点作为分隔符来读取

if ( firstDot != '.'
  || lastDot != '.' )
{
    date.year = date.month = date.day = -1;
}
这里是整个功能:

bool readDate(const std::string &fileName, Date &date)
{
    char firstDot;
    char lastDot;
    std::ifstream f( fileName.c_str() );
    bool toret = false;

    date .year = date.month = date.day = -1;

    if ( f.is_open() ) {
        std::string line;

        // Read line containing the date
        std::getline( f, line );

        //  Chk string
        std::istringstream str( line );

        str >> date.year
             >> firstDot
             >> date.month
             >> lastDot
             >> date.day
        ;

        if ( firstDot != '.'
           || lastDot != '.' )
        {
            date.year = date.month = date.day = -1;
        }
        else toret = true;
    }

    return toret;
}
如您所见,错误条件由函数的返回值以及结构Date的内容发出信号


希望这能有所帮助。

如果您有一个C++0x std::lib(不必太新),这里有一个免费、简单且小的库解决方案(1个源代码,1个头):

以下是使用示例:

#include "date.h"
#include <iostream>
#include <sstream>

int main()
{
    using namespace gregorian;
    date d;
    std::istringstream in("2011.02.07");
    in >> date_fmt("%Y.%m.%d") >> d;
    if (in.fail())
        std::cout << "failed\n";
    else
        std::cout << date_fmt("%A %B %e, %Y") << d << '\n';
}

语法遵循C的strftime函数。日期库需要C++0x标题
,以及对2006年制作的
time\u get
的一些补充。

使用Boost就是一个答案

与此类似,并且有一个非常好的答案,尽管并不完全符合您的问题

#include <fstream>
#include <iostream>
#include <string>
#include <boost/date_time.hpp>

using std::cout;
using std::cin;
using std::endl;
using std::string;
namespace bt = boost::posix_time;

int main()
{
    string dd=" 12 December 2011 15:00:42";
    //string dd="December 2011 15:00:42";
    cout<<dd<<endl;
    std::stringstream time1is(dd);
    std::locale dForm = std::locale(std::locale::classic(),new bt::time_input_facet("%d %B %Y %H:%M:%S"));//12 December 2011 15:00:42

    time1is.imbue(dForm);
    bt::ptime t1;
    if ((time1is>>t1).fail()) {
        std::cerr<<"error while parsing  "<<dd<<std::endl;
    }else{
        std::cerr<<"success!!  "<<dd<<std::endl;
    }
    cout<<t1<<endl;

    }

    //char c;  cin >> c;
    return 0;
}
#包括
#包括
#包括
#包括
使用std::cout;
使用std::cin;
使用std::endl;
使用std::string;
命名空间bt=boost::posix_time;
int main()
{
字符串dd=“2011年12月12日15:00:42”;
//字符串dd=“2011年12月15:00:42”;

感谢您抽出时间解释所有细节。我将尝试一下这段代码是否真的很好???我遇到错误“无法将参数“2”的“tm*”转换为“const char*”,将参数“2”转换为“char*strtime(const char*,const char*,tm*)”我的问题是,我不知道如何使用此函数。现在它工作正常。谢谢:)此库现在已过时。它已被替换为。
#include <fstream>
#include <iostream>
#include <string>
#include <boost/date_time.hpp>

using std::cout;
using std::cin;
using std::endl;
using std::string;
namespace bt = boost::posix_time;

int main()
{
    string dd=" 12 December 2011 15:00:42";
    //string dd="December 2011 15:00:42";
    cout<<dd<<endl;
    std::stringstream time1is(dd);
    std::locale dForm = std::locale(std::locale::classic(),new bt::time_input_facet("%d %B %Y %H:%M:%S"));//12 December 2011 15:00:42

    time1is.imbue(dForm);
    bt::ptime t1;
    if ((time1is>>t1).fail()) {
        std::cerr<<"error while parsing  "<<dd<<std::endl;
    }else{
        std::cerr<<"success!!  "<<dd<<std::endl;
    }
    cout<<t1<<endl;

    }

    //char c;  cin >> c;
    return 0;
}