mfc列表控件写txt文件
时间: 2023-10-05 16:13:45 浏览: 50
你可以使用 MFC 中的 CListCtrl 类来访问和操作列表控件中的数据,然后使用 CStdioFile 类来写入到 TXT 文件中。
下面是一个简单的示例代码:
```cpp
CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST1);
CStdioFile file;
CString strFileName = _T("data.txt");
if (file.Open(strFileName, CFile::modeCreate | CFile::modeWrite))
{
int nItemCount = pListCtrl->GetItemCount();
for (int i = 0; i < nItemCount; i++)
{
CString strItemText;
for (int j = 0; j < pListCtrl->GetHeaderCtrl()->GetItemCount(); j++)
{
CString strHeader;
pListCtrl->GetHeaderCtrl()->GetItem(j, &hdItem);
pListCtrl->GetItemText(i, j, strItemText);
file.WriteString(strItemText + _T("\t"));
}
file.WriteString(_T("\n"));
}
file.Close();
}
```
以上代码将列表控件中的所有数据写入到名为 "data.txt" 的 TXT 文件中。你可以根据需要修改文件名和文件路径等参数。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)