CTime m_Time = CTime::GetCurrentTime();
CString strTime = m_Time.Format("%H:%M:%S");
CString begintime;
begintime = "15:37:26";
if(EstimateTime(begintime,strTime))//比较二者的先后
MessageBox(begintime);
}
结果正确。但是如果
CTime m_Time = CTime::GetCurrentTime();
CString strTime = m_Time.Format("%H:%M:%S");
CString begintime;
begintime = GetTimeTableItemValue(1,3);//从ini表中读出
if(EstimateTime(begintime,strTime))//比较二者的先后
MessageBox(begintime);
}就出错。可能是GetTimeTableItemValue取的值不正确,于是测试:
int i = begintime.GetLength();
CString temp;
temp.Format("%d",i);
if(begintime == "15:37:26")//从表中已知实际上是这个值
{
MessageBox(temp);
}
结果弹出"0".明明begintime == "15:37:26",要不不能进入判断的语句,但是弹出的结果却是“0”长度。该是8才对。难道Format都不正常?于是测试:int i = begintime.GetLength();
CString temp;
i = 8;
temp.Format("%d",i);
if(begintime == "15:37:26")
{
MessageBox(temp);
}
结果弹出"8".现在难道只能说begintime得到了值,但是不和CString 类型一样还是怎么着?但是确实是CString 类型啊。奇怪,大家分析分析...........
CString strTime = m_Time.Format("%H:%M:%S");
CString begintime;
begintime = "15:37:26";
if(EstimateTime(begintime,strTime))//比较二者的先后
MessageBox(begintime);
}
结果正确。但是如果
CTime m_Time = CTime::GetCurrentTime();
CString strTime = m_Time.Format("%H:%M:%S");
CString begintime;
begintime = GetTimeTableItemValue(1,3);//从ini表中读出
if(EstimateTime(begintime,strTime))//比较二者的先后
MessageBox(begintime);
}就出错。可能是GetTimeTableItemValue取的值不正确,于是测试:
int i = begintime.GetLength();
CString temp;
temp.Format("%d",i);
if(begintime == "15:37:26")//从表中已知实际上是这个值
{
MessageBox(temp);
}
结果弹出"0".明明begintime == "15:37:26",要不不能进入判断的语句,但是弹出的结果却是“0”长度。该是8才对。难道Format都不正常?于是测试:int i = begintime.GetLength();
CString temp;
i = 8;
temp.Format("%d",i);
if(begintime == "15:37:26")
{
MessageBox(temp);
}
结果弹出"8".现在难道只能说begintime得到了值,但是不和CString 类型一样还是怎么着?但是确实是CString 类型啊。奇怪,大家分析分析...........
CString temp;
temp.Format("%d",i);
if(begintime == "15:37:26")//从表中已知实际上是这个值
{
MessageBox(temp);
}
这段代码放在什么地方的!
位置是在一个按纽事件中写:(这个按纽专门用来测试某些新加的代码的,呵呵)
CTime m_Time = CTime::GetCurrentTime();
CString strTime = m_Time.Format("%H:%M:%S");
CString begintime;
begintime = GetTimeTableItemValue(1,3);//从ini表中读出
int i = begintime.GetLength();
CString temp;
temp.Format("%d",i);
if(begintime == "15:37:26")//从表中已知实际上是这个值
{
MessageBox(temp);
}
CString temp;
temp.Format("%d",i);
if(begintime == "15:37:26")//从表中已知实际上是这个值
{
MessageBox(temp);
}
////////////////////////////////////
代码中的int i = begintime.GetLength();begintime是多少?会不会从GetTimeTableItemValue得值得时候就失败了?建议单步跟踪一下。
CTime m_Time = CTime::GetCurrentTime();
CString strTime = m_Time.Format("%H:%M:%S");
CString begintime;
begintime = GetTimeTableItemValue(1,3);//从ini表中读出
if(EstimateTime(begintime,strTime))//比较二者的先后
MessageBox(begintime);
}这个好判断啊!
begintime读出后用MessageBox(begintime);看结果就是了
CTime m_Time = CTime::GetCurrentTime();
CString strTime = m_Time.Format("%H:%M:%S");
CString begintime;
begintime = GetTimeTableItemValue(1,3);//从ini表中读出
MessageBox(begintime);
if(EstimateTime(begintime,strTime))//比较二者的先后
MessageBox(begintime);
}
看结果是"15:37:26",所以进了判断的内部,但是长度却是零。
GetLength()不会有问题吧?用别的字符穿测试都是正确的。
int i = begintime.GetLength();
CString temp;
temp.Format("%d",i);
MessageBox(temp);
结果是8begintime = GetTimeTableItemValue(1,3);//从ini表中读出
int i = begintime.GetLength();
CString temp;
temp.Format("%d",i);
if(begintime == "15:37:26")//从表中已知实际上是这个值
{
MessageBox(temp);
}
结果是0怎么这样?
begintime = GetTimeTableItemValue(1,3);//从ini表中读出这句后面看
是不是在函数里是一个char数组,然后转换为CString的。
CString C.........Dlg::GetTimeTableItemValue(int itemnum, int paramnum)
{
char m_csPath[255];CString m_strPath;
GetCurrentDirectory(255 ,(LPTSTR)m_csPath);
m_strPath.Format("%s",m_csPath);
m_strPath = m_strPath + "\\timetable.ini";
CString item;
item.Format("%d",itemnum);
CString temp;
temp.Format("%d",paramnum);
CString value;
::GetPrivateProfileString("item"+item,"param"+temp,"default", value.GetBuffer(10),10,m_strPath);
return value;
}
如果:
begintime = GetTimeTableItemValue(1,3);//从ini表中读出
MessageBox(begintime);
结果是"15:37:26"正常弹出。
所以进了判断if(begintime == "15:37:26")//从表中已知实际上是这个值
{
MessageBox(temp);
}却得到长度为0的结果..................
CString begintime;
begintime = GetTimeTableItemValue(1,3);
MessageBox(begintime);
//if(begintime == "15:37:26")
//{
// begintime = "15:37:26";
//}
int i = begintime.GetLength();
CString temp;
temp.Format("%d",i);
if(begintime == "15:37:26")
{
MessageBox(temp);
}
两个弹出分别为:"15:37:26","0".
如果去掉注释,两个弹出分别为:"15:37:26","8".真是奇怪了,GetTimeTableItemValue的原型在上面,大家看看。同时timetable.ini的结构如下:
[item1]
param1=卡1
param2=第1路
param3=15:37:26
param4=15:38:26
param5=周三
param6=否
[item2]
param1=卡1
param2=第1路
param3=10:01:26
param4=12:01:26
param5=周一
param6=否
[itemcount]
count=2
::GetPrivateProfileString("item"+item,"param"+temp,"default", value.GetBuffer(10),10,m_strPath);
后面加上value.ReleaseBuffer();
strcpy (str.GetBuffer (10), "China");
CString msg;
msg.Format ("%s:%d", str, str.GetLength ());
AfxMessageBox (msg); // 《==输出“china:0”
str.ReleaseBuffer ();
msg.Format ("%s:%d", str, str.GetLength ());
AfxMessageBox (msg); // 《==输出“china:5”
{
if( nNewLength == -1 )
{
nNewLength = StringLength( m_pszData );
}
SetLength( nNewLength );
}
static int __cdecl StringLength( const char* psz ) throw()
{
if( psz == NULL )
{
return( 0 );
}
return( int( strlen( psz ) ) );
}
If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CSimpleStringT member methods.
这包括你调用的“==”。