| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 624 人关注过本帖
标题:[求助]急!请大家看下这个存储日期和时间的程序,并有问题请教
只看楼主 加入收藏
单调黑白
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2007-10-4
收藏
 问题点数:0 回复次数:4 
[求助]急!请大家看下这个存储日期和时间的程序,并有问题请教

//LF_DateTime.h

#include <time.h>

class CLF_DateTime
{
private:
struct tm *local;
time_t t;
char str[80];
public:
CLF_DateTime(int year, int month, int day,int hour, int min,int sec);
~CLF_DateTime(void);
void display(char* strdisplay);
};


//LF_DateTime.cpp

#include "LF_DateTime.h"
#include <time.h>
#include <iostream>

using namespace std;

CLF_DateTime::CLF_DateTime(int year, int month, int day,int hour, int min,int sec)
{
local = new tm;
local->tm_year = year;
local->tm_mon = month;
local->tm_mday = day;
local->tm_hour =0;
local->tm_min =0;
local->tm_sec =0;

}

CLF_DateTime::~CLF_DateTime(void)
{
delete(local);
}

void CLF_DateTime::display(char* strdisplay)
{

strftime(str,80,"%Y-%m-%d %H:%M:%S",local);
cout<<str<<endl;

}
int _tmain(int argc, _TCHAR* argv[])
{
CLF_DateTime *pa;
char testdisplay[256];


pa = new CLF_DateTime(2005,10 ,01 , 11,50,23);
pa->display(testdisplay);

cout<<testdisplay<<endl;

return 0;
}

编译可以通过!但执行后出现的都是乱码!我想要的结果应该是"2005-10-01 11:50:23"

搜索更多相关主题的帖子: int DateTime CLF include 
2007-11-07 16:38
单调黑白
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2007-10-4
收藏
得分:0 
回复:(单调黑白)[求助]急!请大家看下这个存储日期和...
请大家帮帮忙啊 谢谢了!

雕刻单调的人生
2007-11-08 10:08
lxm520910
Rank: 1
等 级:新手上路
帖 子:96
专家分:0
注 册:2007-10-3
收藏
得分:0 
你形参传的是数组名,它能反回来的话,就越界了吧?用引用试下看
2007-11-08 10:40
yijing21
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-11-7
收藏
得分:0 

我修改后如下 可以正常显示
//LF_DateTime.h

class CLF_DateTime
{
private:
struct tm *local;
time_t t;
// char str[80];
public:
CLF_DateTime(int year, int month, int day,int hour, int min,int sec);
~CLF_DateTime(void);
void display(char * strdisplay);
};


//LF_DateTime.cpp


#include <time.h>
#include <iostream>

using namespace std;

CLF_DateTime::CLF_DateTime(int year, int month, int day,int hour, int min,int sec)
{
local = new tm;
local->tm_year = year-1900;//tm_year是从1900年起开始计算 也就是说他显示2007时tm_year值并不是2007
local->tm_mon = month-1;//月份可见definition里有说明是[0,11]所以比现实小了一个月
local->tm_mday = day;
local->tm_hour =hour;
local->tm_min =min;
local->tm_sec =sec;

}

CLF_DateTime::~CLF_DateTime(void)
{
delete(local);
}

void CLF_DateTime::display(char * strdisplay)
{

strftime(strdisplay,80,"%Y-%m-%d %H:%M:%S",local);//这里并未用到str[80]
cout<<strdisplay<<endl;


}

int main()
{
CLF_DateTime *pa;
char testdisplay[256];


pa = new CLF_DateTime(2005,10,01,11,50,23);
pa->display(testdisplay);
cout<<testdisplay<<endl;//重复输出

return 0;
}

2007-11-08 13:56
zwh2698
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2005-9-24
收藏
得分:0 

// ddaet.cpp : Defines the entry point for the console application.
//

#include "StdAfx.h"
#include <time.h>
#include <iostream>
#include <tchar.h>
#include <string>

using namespace std;

#include <time.h>

class CLF_DateTime
{
private:
struct tm *local;
time_t t;
char str[80];
public:
CLF_DateTime(int year, int month, int day,int hour, int min,int sec);
~CLF_DateTime(void);
void display(char* strdisplay);
};


CLF_DateTime::CLF_DateTime(int year, int month, int day,int hour, int min,int sec)
{
local = new tm;
local->tm_year = year -1900;
local->tm_mon = month;
local->tm_mday = day;
local->tm_hour =0;
local->tm_min =0;
local->tm_sec =0;

}

CLF_DateTime::~CLF_DateTime(void)
{
delete(local);
}

void CLF_DateTime::display(char* strdisplay)
{

strftime(strdisplay,256,"%Y-%m-%d %H:%M:%S",local);

cout<<strdisplay<<endl;


}

int main(int argc, _TCHAR* argv[])
{

CLF_DateTime *pa;
char testdisplay[256];

pa = new CLF_DateTime(2005,10 ,01 , 11,50,23);
pa->display(testdisplay);

cout<<testdisplay<<endl;

return 0;
}


2007-11-08 18:38
快速回复:[求助]急!请大家看下这个存储日期和时间的程序,并有问题请教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011090 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved