qq_33041689
qq_33041689
2017-04-21 13:09

mfc中将txt文件的内容读到listcontrol时不能循环读入的问题

20
  • txt
  • mfc
  • list
  • 异常

BOOL CTsgDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

m_ListCtrl1.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_CHECKBOXES);
m_ListCtrl1.InsertColumn(0, L" ", LVCFMT_LEFT, 20);
m_ListCtrl1.InsertColumn(1, L"序号", LVCFMT_LEFT, 80);
m_ListCtrl1.InsertColumn(2, L"时间", LVCFMT_LEFT, 120);
m_ListCtrl1.InsertColumn(3, L"编号", LVCFMT_LEFT, 80);
m_ListCtrl1.InsertColumn(4, L"FID", LVCFMT_LEFT, 80);
m_ListCtrl1.InsertColumn(5, L"内容", LVCFMT_LEFT, 120);
m_ListCtrl1.InsertColumn(6, L"LID", LVCFMT_LEFT, 80);
m_ListCtrl1.InsertColumn(7, L"内容", LVCFMT_LEFT, 120);
m_ListCtrl1.InsertColumn(8, L"TID", LVCFMT_LEFT, 80);
m_ListCtrl1.InsertColumn(9, L"内容", LVCFMT_LEFT, 120);
m_ListCtrl1.InsertColumn(10, L"记录", LVCFMT_LEFT, 300);

char* old_locale = _strdup(setlocale(LC_CTYPE, NULL));
setlocale(LC_CTYPE, "chs");
CStdioFile file;
file.Open(TEXT("TSG.txt"), CFile::modeRead);

CString s;
int n = 0;//控制行数,具体你自己定
while (TRUE == file.ReadString(s))
{
    if (s.Find(L" ") == -1 && s.GetLength() <= 100)
        continue;
    else

    {
        CString s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11;
        int pos = -1;
        pos = s.Find(L" ");
        s1 = s.Left(pos);
        s = s.Right(s.GetLength() - pos-1);
        pos = s.Find(L" ");
        s2 = s.Left(pos);
        s = s.Right(s.GetLength() - pos - 1);
        pos = s.Find(L";");
        s3 = s.Left(pos);
        s = s.Right(s.GetLength() - pos - 1);
        pos = s.Find(L" ");
        s4 = s.Left(pos);
        s = s.Right(s.GetLength() - pos-1);
        pos = s.Find(L":");
        s5 = s.Left(pos);
        s = s.Right(s.GetLength() - pos - 1);
        pos = s.Find(L"L");
        s6 = s.Left(pos);
        s = s.Right(s.GetLength() - pos);
        pos = s.Find(L":");
        s7 = s.Left(pos);
        s = s.Right(s.GetLength() - pos - 1);
        pos = s.Find(L"T");
        s8 = s.Left(pos);
        s = s.Right(s.GetLength() - pos);
        pos = s.Find(L":");
        s9 = s.Left(pos);
        s = s.Right(s.GetLength() - pos - 1);
        pos = s.Find(L" ");
        s10 = s.Left(pos);
        s = s.Right(s.GetLength() - pos-1);
        s11 = s.Right(s.GetLength());

        m_ListCtrl1.InsertItem(n, s1);
        m_ListCtrl1.SetItemText(n, 1, s2);
        m_ListCtrl1.SetItemText(n, 2, s3);
        m_ListCtrl1.SetItemText(n, 3, s4);
        m_ListCtrl1.SetItemText(n, 4, s5);
        m_ListCtrl1.SetItemText(n, 5, s6);
        m_ListCtrl1.SetItemText(n, 6, s7);
        m_ListCtrl1.SetItemText(n, 7, s8);
        m_ListCtrl1.SetItemText(n, 8, s9);
        m_ListCtrl1.SetItemText(n, 9, s10);
        m_ListCtrl1.SetItemText(n, 10, s11);
        n++;
    }

}
setlocale(LC_CTYPE, old_locale);
free(old_locale);// TODO:  在此添加额外的初始化

return TRUE;  // return TRUE unless you set the focus to a control
// 异常:  OCX 属性页应返回 FALSE

}
以上为代码
图片说明
上图为txt文件内容
图片说明
上图为运行结果

为什么不能正常读入?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答