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;
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;
}
}
1 其他线程改了
2 指针的使用错误
3 某些系统类库有bug导致非法内存读写
程序主要就是寻找打印起始时刻的序号
1 其他线程改了
2,楼主可能是在Release下调试吧.
long print_from_col_num;
long print_to_col_num;
移到最上面,试试。
strto又变成0了
strfrom好好的
strto不行
改成
char pTimeBuf[128];
建议一般把数组弄大点,以免出错.
那么,长度还要考虑换行字符.
一般换行是 '\r' '\n', 所以
int strlength = strlen("2000-01-01 00:00") + 2;
char pTimeBuf[16];就可以了
以前我也是设置16,但出现"2000-01-01 00:00?//?";乱码,所以我把它改成4,并LEFT(16),就出现上面问题了
改成 char *pcolBuf = NULL;
试试
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);就明显越界了。
最好改为char pcolBuf[11];因为后面读取的时候是最大长度为10,为防止读满10个字符后没有结束符,所以声明为11。