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 类型啊。奇怪,大家分析分析...........

解决方案 »

  1.   

    int i = begintime.GetLength();
    CString temp;
    temp.Format("%d",i);
    if(begintime == "15:37:26")//从表中已知实际上是这个值
    {
    MessageBox(temp);
    }
    这段代码放在什么地方的!
      

  2.   

    我真是郁闷的头大,我把自己分析的流程都写出来了,但是既然长度为零是怎么进的if(begintime == "15:37:26")的判断的,鬼
      

  3.   

    to:: laiyiling(最熟悉的陌生人) 
    位置是在一个按纽事件中写:(这个按纽专门用来测试某些新加的代码的,呵呵)
    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);
    }
      

  4.   

    int i = begintime.GetLength();
    CString temp;
    temp.Format("%d",i);
    if(begintime == "15:37:26")//从表中已知实际上是这个值
    {
    MessageBox(temp);
    }
    ////////////////////////////////////
    代码中的int i = begintime.GetLength();begintime是多少?会不会从GetTimeTableItemValue得值得时候就失败了?建议单步跟踪一下。
      

  5.   

    begintime 等于"15:37:26",否则进不了判断,也没有可能弹出对话框。
      

  6.   

    但是如果
    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);
    }
      

  7.   

    begintime读出后用MessageBox(begintime);
    看结果是"15:37:26",所以进了判断的内部,但是长度却是零。
    GetLength()不会有问题吧?用别的字符穿测试都是正确的。
      

  8.   

    begintime = "15:37:26";
    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怎么这样?
      

  9.   

    把MessageBox放在
    begintime = GetTimeTableItemValue(1,3);//从ini表中读出这句后面看
      

  10.   

    GetTimeTableItemValue要看看你这个函数的实现才知道。
      

  11.   

    看了半天没看出来为什么!你的GetTimeTableItemValue函数是自己写的函数吗??
      

  12.   

    直接担心你在GetTimeTableItemValue这个函数里是怎样返回字符串的。
    是不是在函数里是一个char数组,然后转换为CString的。
      

  13.   

    GetTimeTableItemValue原型:
    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的结果..................
      

  14.   

    看下面的测试:
    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
      

  15.   

    问题在于你对INI读取的时候出错,你的CString没有释放。
    ::GetPrivateProfileString("item"+item,"param"+temp,"default", value.GetBuffer(10),10,m_strPath);
    后面加上value.ReleaseBuffer();
      

  16.   

    最好不要用CStrnig直接读取,可以用一个数组得到数据。
      

  17.   

    感谢 holyeagle(一杯清茶) ,果然如此。不过很迷惑,内存泄露怎么和这个搭上的?请教
      

  18.   

    没有调用ReleaseBuffer ()的缘故。CString str;
    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”
      

  19.   

    你直接操作CString分配的内存,它根本不知道你用了多大。用完后调用ReleaseBuffer才重新计算长度。下面是CString源码中的片断: void ReleaseBuffer( int nNewLength = -1 )
    {
    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 ) ) );
    }
      

  20.   

    VC2003版中的MFC,ReleaseBuffer 完成的并非是释放内存,而是重新计算长度。MSDN中的:
    If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CSimpleStringT member methods.
    这包括你调用的“==”。