读书人

【奇怪了 Left Right GetLength 都不起

发布时间: 2012-04-19 14:36:43 作者: rapoo

【奇怪了 Left Right GetLength 都不起作用】

C/C++ code
void CMy20120417mfcDlg::ReadData() {    // TODO: Add your control notification handler code here    CString str;         //CFileDialog fileDlg(TRUE);     //if(fileDlg.DoModal() == IDOK)     //{         CFile file;         //file.Open(fileDlg.GetPathName(),CFile::modeReadWrite);        file.Open("data.txt",CFile::modeReadWrite);                file.Read(str.GetBuffer(file.GetLength()),(UINT)file.GetLength());         //GetDlgItem(  )->SetWindowText(str);         file.Close();    //}        int len = str.GetLength();    CString t;    t.Format("%d",len);    AfxMessageBox(t);    char ch[10000] = {0};        char *start,*tail;    char tmp[100]={0};    int index=1,j=1;    memcpy(ch,str,len);    start=tail=ch;    CString tmp1;    tmp1.Format("%d",index);    int nRow = m_List.InsertItem(index, tmp1);//插入行    while (*start!='\0')    {        if (*start=='<' )        {            while ( *tail!='\0')            {                if (*tail=='>')                {                    start++;                    strncpy(tmp,start,tail-start);                    m_List.SetItemText(nRow, j++, tmp);//设置数据                     if (j==10)                    {                        j=1;                        index++;                        tmp1.Format("%d",index);                        nRow = m_List.InsertItem(index, tmp1);//插入行                    }                    tail++;                    start = tail;                    break;                }                tail++;            }        }        start++;    }    AfxMessageBox(str);        ::MessageBox(NULL,"找到","Warnning",MB_OK);            //写入数据//     for (index=1;index<=10;index++)//     {//         CString tmp;//         tmp.Format("%d",index);//         int nRow = m_List.InsertItem(index, tmp);//插入行//         for (int j=1;j<=10;j++)//         {//             m_List.SetItemText(nRow, j, "text");//设置数据 //         } //     }}


data.txt文件的内容格式如下
1: <1001.3,26.4> <1002.3,26.7> <1003.3,26.7> <1004,26.7> <1005.3,26.7> <1006.3,26.7> <1007.3,26.7> <1008.3,26.7> <1009.3,26.7> <1010.3,26.7>
2: <998.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7>
3: <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7>
4: <998.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7>
5: <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7>
6: <998.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7> <1002.3,26.7>

就是想把这些数据都填充到 List里

结果神奇了 从文件读取的str

Left Right 甚至连GetLength 都不吃

用Find找到\r\n换行符 准备以换行符为位置拆分

结果 不管是Left 还是Right 都拆不开

用GetLength 直接连长度也是0

但是输出的确是文件里全部内容

[解决办法]
CString str;

//CFileDialog fileDlg(TRUE);
//if(fileDlg.DoModal() == IDOK)
//{
CFile file;
//file.Open(fileDlg.GetPathName(),CFile::modeReadWrite);


file.Open("data.txt",CFile::modeReadWrite);

file.Read(str.GetBuffer(file.GetLength()),(UINT)file.GetLength());
//GetDlgItem( )->SetWindowText(str);
file.Close();
//}

【str.ReleaseBuffer();】

int len = str.GetLength();

see msdn

读书人网 >VC/MFC

热点推荐