怎么让txt中的文件分行显示在listbox中
如何让txt中的文件分行显示在listbox中
比如我 test.txt中有下列数据
姓名 ip
张三 196.185.12.1
李四 198.64.1.0
我要如何把它也按行显示在listbox中呢
下面是我写代码
CFile m_File;
CString str;
CFileException e;
TCHAR pbuf[100];
BOOL bopened = m_File.Open(_T( "e:\\test.txt "),CFile::modeRead,&e);
if(!bopened)
{
MessageBox(_T ( "FILE CAN 'T OPEN! "),_T ( "Error "),MB_OK);
return;
}
else
{
while(m_File.Read(pbuf, 100))
{
test.AddString(pbuf);
}
m_File.Close();
}
但是现实都在一行,要如何才能分行显示。
------解决方案--------------------
你可以使用CStdioFile的ReadString,这样每次就可以从文本文件中读取一行了。
然后巴你读取出来的行 add 到 listbox 中即可。
------解决方案--------------------
int m=0,a=0;
listbox.InsertColumn(0, "name ",LVCFMT_LEFT,50);
listbox.InsertColumn(1, "ip ",LVCFMT_LEFT,100);
//用一个2维数组存数据表
//a 0 1
//0 张三 196.185.12.1
//1 李四 198.64.1.0
for(int i=0;i <2;i++)
{
a=listbox.InsertItem(m,a[i][m],LVCFMT_LEFT);
for(int j=0;j <2;j++)
{
listbox.SetItemText(a,j,fContext[m][j]);
}
m++;
}
比如我 test.txt中有下列数据
姓名 ip
张三 196.185.12.1
李四 198.64.1.0
我要如何把它也按行显示在listbox中呢
下面是我写代码
CFile m_File;
CString str;
CFileException e;
TCHAR pbuf[100];
BOOL bopened = m_File.Open(_T( "e:\\test.txt "),CFile::modeRead,&e);
if(!bopened)
{
MessageBox(_T ( "FILE CAN 'T OPEN! "),_T ( "Error "),MB_OK);
return;
}
else
{
while(m_File.Read(pbuf, 100))
{
test.AddString(pbuf);
}
m_File.Close();
}
但是现实都在一行,要如何才能分行显示。
------解决方案--------------------
你可以使用CStdioFile的ReadString,这样每次就可以从文本文件中读取一行了。
然后巴你读取出来的行 add 到 listbox 中即可。
------解决方案--------------------
int m=0,a=0;
listbox.InsertColumn(0, "name ",LVCFMT_LEFT,50);
listbox.InsertColumn(1, "ip ",LVCFMT_LEFT,100);
//用一个2维数组存数据表
//a 0 1
//0 张三 196.185.12.1
//1 李四 198.64.1.0
for(int i=0;i <2;i++)
{
a=listbox.InsertItem(m,a[i][m],LVCFMT_LEFT);
for(int j=0;j <2;j++)
{
listbox.SetItemText(a,j,fContext[m][j]);
}
m++;
}