我的代码如下: char ch1[20];
char ch2[20],ch3[20];
CString stra="MyName,IS,Pan110"; memset(ch1,0,20);
memset(ch2,0,20); sscanf(stra,"%s,%s,%s",ch1,ch2,ch3); MessageBox(ch1);
MessageBox(ch2);
MessageBox(ch3); 可为什么,ch1显示“MyName,IS,Pan110”
ch2="";
ch3="";
但是,为什么下面这段代码可以呢? CString strTemp ="2,36KB,0:13:32.93,[Unknow]"; int iCountIndex,iMemory,iHour,iMinute,iSecond,iMillisecond;
CString iFileName; sscanf(strTemp,"%d,%dKB,%d:%d:%d.%d,% s",&iCountIndex,&iMemory,&iHour,&iMinute,&iSecond,&iMillisecond,iFileName);
MessageBox(iFileName);
TRACE("%d,%d,%d,%d",iCountIndex,iMemory,iHour,iMinute);
char ch2[20],ch3[20];
CString stra="MyName,IS,Pan110"; memset(ch1,0,20);
memset(ch2,0,20); sscanf(stra,"%s,%s,%s",ch1,ch2,ch3); MessageBox(ch1);
MessageBox(ch2);
MessageBox(ch3); 可为什么,ch1显示“MyName,IS,Pan110”
ch2="";
ch3="";
但是,为什么下面这段代码可以呢? CString strTemp ="2,36KB,0:13:32.93,[Unknow]"; int iCountIndex,iMemory,iHour,iMinute,iSecond,iMillisecond;
CString iFileName; sscanf(strTemp,"%d,%dKB,%d:%d:%d.%d,% s",&iCountIndex,&iMemory,&iHour,&iMinute,&iSecond,&iMillisecond,iFileName);
MessageBox(iFileName);
TRACE("%d,%d,%d,%d",iCountIndex,iMemory,iHour,iMinute);
CString strLine = "MyName,IS,Pan110";
CString strTemp;
int nCount=0; char *ss=strLine.GetBufferSetLength(strLine.GetLength());
char *p;
for ( p=strtok(ss, ","); p!=NULL; p=strtok(NULL, ",") )
{
nCount++;
switch(nCount)
{
case 1:
strcpy(ch1, p);
break;
case 2:
strcpy(ch2, p);
break;
case 3:
strcpy(ch3 ,p);
break;
default:
break;
}
}
而%s是字符串,sscanf查打字符串的时候是以\0为结尾的,当然一次性就把"MyName,IS,Pan110";查找完了,并放到ch1里,所以ch2和ch3里就没东东了