VC下一个long型变量已经赋成110了,又执行了几步,没用它,就变成807416882,想用已经变了,怎么回事,请高手指教?

解决方案 »

  1.   

    可能是其它变量改变了long型变量地址的内容了。
      

  2.   

    CString strDate;
             CTime  time = CTime::GetCurrentTime();    
             strDate.Format(_T("%02d-%02d-%02d "),time.GetYear(),time.GetMonth(),
                                         time.GetDay());
             CString strfrom,strfromhour;
             strfromhour.Format("%02d:%02d",PrintHourFrom,PrintMinuteFrom);
             strfrom=strDate+strfromhour;         CString strto,strtohour;
             strtohour.Format("%02d:%02d",PrintHourTo,PrintMinuteTo);
             strto=strDate+strtohour;
     
             long print_from_col_num;
             long print_to_col_num;         char pTimeBuf[4];
             memset(pTimeBuf,0,4);
             CString pTimeBuf1,pTimeBuf2;
             char pcolBuf[10];
             memset(pcolBuf,0,10);
             long i;
    long j=0;         CFile filetime("e:\\time.txt",CFile::modeRead);
    int strlength=strlen("2000-01-01 00:00");
    long filelength=filetime.GetLength();
    long row=filelength/strlength;
    row+=1;
     
             CFile filecol_num("e:\\col_num.txt",CFile::modeRead);
             
    for( i=1;i<row;i++)
    {
                 j++;  
        filetime.Seek(0-j*16,CFile::end);
                 filetime.Read(pTimeBuf,strlength);
                 pTimeBuf1= pTimeBuf;
        pTimeBuf2=pTimeBuf1.Left(16);
                 if((strfrom.Compare(pTimeBuf2)==0)||(strfrom.Compare(pTimeBuf2)>0))
        {    
                      filecol_num.Seek(0-j*10,CFile::end);
                      filecol_num.Read(pcolBuf,10);
                      print_from_col_num=atol(pcolBuf); 
    filetime.Close();
    filecol_num.Close();
    break;
         }
             }         CFile filetimeto("e:\\time.txt",CFile::modeRead);
             CFile filecol_numto("e:\\col_num.txt",CFile::modeRead);  
    j=0;
             for( i=1;i<row;i++)
    {
        j++;
        filetimeto.Seek(0-j*16,CFile::end);
                 filetimeto.Read(pTimeBuf,strlength);
                 pTimeBuf1=pTimeBuf;
       pTimeBuf2=pTimeBuf1.Left(16);
                 if((strto.Compare(pTimeBuf2)==0)||(strto.Compare(pTimeBuf2)>0))
       {
                      filecol_numto.Seek(long(0-j*10),CFile::end);
                      filecol_numto.Read(pcolBuf,10);
                      print_to_col_num=atol(pcolBuf); 
                      filetimeto.Close();
                      filecol_numto.Close();
    break;  
        }
    }
      

  3.   

    可能的原因:
    1 其他线程改了
    2 指针的使用错误
    3 某些系统类库有bug导致非法内存读写
      

  4.   

    就是print_from_col_num出问题
    程序主要就是寻找打印起始时刻的序号
      

  5.   

    可能的原因:
    1 其他线程改了
    2,楼主可能是在Release下调试吧.
      

  6.   


    long print_from_col_num;
    long print_to_col_num;
    移到最上面,试试。
      

  7.   

    移到最上面
    strto又变成0了
      

  8.   

    提示一下:807416882十六进制是30 20 34 32,很明显是ASCII码字符串"0 42"
      

  9.   

    移到最上面
    strfrom好好的
    strto不行
      

  10.   

    把atol 里的pcolBuf打印出来看看?我觉得这里可能有问题
      

  11.   

    char pTimeBuf[4];int strlength=strlen("2000-01-01 00:00");filetimeto.Read(pTimeBuf,strlength);显然 pTimeBuf 太小
    改成
    char pTimeBuf[128];
      

  12.   

    其他的 char xxx[N] 我没仔细看,
    建议一般把数组弄大点,以免出错.
      

  13.   

    另外,如果文件中,每行一个 2000-01-01 00:00
    那么,长度还要考虑换行字符.
    一般换行是 '\r' '\n', 所以
     int strlength = strlen("2000-01-01 00:00") + 2;
      

  14.   

    就是pTimeBuf 太小,
    char pTimeBuf[16];就可以了
    以前我也是设置16,但出现"2000-01-01 00:00?//?";乱码,所以我把它改成4,并LEFT(16),就出现上面问题了
      

  15.   

    可以把        char pcolBuf[10];
    改成          char *pcolBuf = NULL;
    试试
      

  16.   

    就是pTimeBuf 太小,
    char pTimeBuf[16];就可以了
    以前我也是设置16,但出现"2000-01-01 00:00?//?";乱码,所以我把它改成4,并LEFT(16),就出现上面问题了
    把pTimeBuf声明为char [17],16之所以出现“"2000-01-01 00:00?//?";乱码”是因为“2000-01-01 00:00”的长度是16,最后没有结束符\0。如果声明为char[4]的话,filetimeto.Read(pTimeBuf,strlength);就明显越界了。
      

  17.   

    char pcolBuf[10];
    最好改为char pcolBuf[11];因为后面读取的时候是最大长度为10,为防止读满10个字符后没有结束符,所以声明为11。