如何将List Control中的内容导入到 .txt文件(可用记事本打开) 如上,最好有例子。 谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 做一个循环阿,读取一个Item的内容到CString中,然后写入文件中,看是否还能取到Item,如果可以就重复上述操作,不能就结束。 好像只能自己一个个取出来写到文件里。BOOL CNSListCtrl::SaveItemToFile(FILE* fp){ if(fp == NULL) { return FALSE; } TCHAR szText[256]; LV_COLUMN lvc; lvc.mask = LVCF_TEXT | LVCF_FMT | LVCF_WIDTH; lvc.pszText = szText; lvc.cchTextMax = sizeof(szText); CString strText; CDWordArray ayFormat; WORD wJustify; for(int nColumn = 0; GetColumn(nColumn, &lvc); nColumn++) { wJustify = ST_LEFT; switch(lvc.fmt & LVCFMT_JUSTIFYMASK) { case LVCFMT_RIGHT: wJustify = ST_RIGHT; break; case LVCFMT_CENTER: wJustify = ST_CENTER; break; } ayFormat.Add(MAKELONG(wJustify, (WORD)lvc.cx)); strText += FormatColumnString(szText, ayFormat[nColumn]); strText += _T("\t"); } if(strText.IsEmpty()) { return FALSE; } strText += _T("\r\n"); _ftprintf(fp, strText); for(int i = 0; i < GetItemCount(); i++) { strText.Empty(); for(int j = 0; j < nColumn; j++) { strText += FormatColumnString(GetItemText(i, j), ayFormat[j]); strText += _T("\t"); } strText += _T("\r\n"); _ftprintf(fp, strText); } return TRUE;} 文件那一部分具体如何操作呢?比如,如何将“abcdefg"写入到一个。txt,都需那些函数? wwwsq(wwwsq) 已经告诉你答案了。关于写文件,还可以直接使用API函数:如OpenFile和WriteFile等。 向文件中写就用CFile类中的WriteFile()函数。 很多的函数_open()_write()_close()就可以其他如CFILE,CSTDFILE,FILE类都可以完成 谢谢各位!本人最后采用的方法如下,功能可以实现,不知有无不妥之处!//按下导出按钮后的处理函数void CFileDlg::OnButtonExport() { //从保存对话框中输入要保存到的文件和路径 CString strFileName; CFileDialog m_ldFile(FALSE); m_ldFile.m_ofn.lpstrFilter = "*.TXT"; m_ldFile.m_ofn.lpstrDefExt = "TXT"; if (m_ldFile.DoModal() == IDOK) { strFileName = m_ldFile.GetPathName(); //包括了路径和文件名 } //创建文件 char* pszFileName = strFileName.GetBuffer(strFileName.GetLength()); CStdioFile myFile; CFileException fileException; if ( !myFile.Open( pszFileName, CFile::modeCreate | CFile::modeWrite ), &fileException ) { TRACE( "Can't open file %s, error = %u\n", pszFileName, fileException.m_cause ); } //文件中写入内容 int iColSum = 3; //List中的栏数 int iCount = m_List.GetItemCount(); for (int iItem = 0; iItem < iCount; iItem++) for (int iCol = 0; iCol < iColSum; iCol++) { CString strTemp = m_List.GetItemText( iItem, iCol); if (iCol == 0) lstrcat(strTemp.GetBuffer(strTemp.GetLength()), "\t"); else lstrcat(strTemp.GetBuffer(strTemp.GetLength()), "\n"); myFile.WriteString(strTemp.GetBuffer(strTemp.GetLength())); } //是否立即查看该文件 if (MessageBox("已保存,要查看吗", "完成", MB_YESNO) == IDYES) { ShellExecute(NULL, "open", pszFileName, NULL, NULL, SW_SHOWNORMAL); } //关闭文件 myFile.Close();} 保存的时候最好保存左右对齐格式和数据长度。这样保存好的text文件才方便查看。 如何通过ocx界面上的按钮来关闭网页 求教Outlook 2007类似界面实现 求MFC毕业设计题目? 在VC中如何截获对话框关闭事件,并取消关闭命令? 有没有系统的介绍ie编程的书籍或者资料 找了好久都没看到 谁能介绍一下 谢谢! MFC Dialog程序启动时如何隐藏? DeviceIoControl读取网卡序列号的问题 ****快来啊!2000分求解.密码问题.在线等待**** 有谁知道类似pwsniffer(窃取密码)这样的软件是什么原理编的? 急!请帮忙! 几个小问题 在程序内部,我如何取得鼠标下的词,即进程内的鼠标取词 十分棘手,恳请各位帮忙.小弟先谢了.
{
if(fp == NULL)
{
return FALSE;
} TCHAR szText[256];
LV_COLUMN lvc;
lvc.mask = LVCF_TEXT | LVCF_FMT | LVCF_WIDTH;
lvc.pszText = szText;
lvc.cchTextMax = sizeof(szText);
CString strText;
CDWordArray ayFormat;
WORD wJustify;
for(int nColumn = 0; GetColumn(nColumn, &lvc); nColumn++)
{
wJustify = ST_LEFT;
switch(lvc.fmt & LVCFMT_JUSTIFYMASK)
{
case LVCFMT_RIGHT:
wJustify = ST_RIGHT;
break;
case LVCFMT_CENTER:
wJustify = ST_CENTER;
break;
}
ayFormat.Add(MAKELONG(wJustify, (WORD)lvc.cx));
strText += FormatColumnString(szText, ayFormat[nColumn]);
strText += _T("\t");
}
if(strText.IsEmpty())
{
return FALSE;
}
strText += _T("\r\n");
_ftprintf(fp, strText);
for(int i = 0; i < GetItemCount(); i++)
{
strText.Empty();
for(int j = 0; j < nColumn; j++)
{
strText += FormatColumnString(GetItemText(i, j), ayFormat[j]);
strText += _T("\t");
}
strText += _T("\r\n");
_ftprintf(fp, strText);
}
return TRUE;
}
_open()
_write()
_close()
就可以
其他如CFILE,CSTDFILE,FILE类都可以完成
本人最后采用的方法如下,功能可以实现,不知有无不妥之处!//按下导出按钮后的处理函数
void CFileDlg::OnButtonExport()
{
//从保存对话框中输入要保存到的文件和路径
CString strFileName;
CFileDialog m_ldFile(FALSE); m_ldFile.m_ofn.lpstrFilter = "*.TXT";
m_ldFile.m_ofn.lpstrDefExt = "TXT"; if (m_ldFile.DoModal() == IDOK)
{
strFileName = m_ldFile.GetPathName(); //包括了路径和文件名
} //创建文件
char* pszFileName = strFileName.GetBuffer(strFileName.GetLength());
CStdioFile myFile;
CFileException fileException; if ( !myFile.Open( pszFileName, CFile::modeCreate |
CFile::modeWrite ), &fileException )
{
TRACE( "Can't open file %s, error = %u\n",
pszFileName, fileException.m_cause );
}
//文件中写入内容
int iColSum = 3; //List中的栏数
int iCount = m_List.GetItemCount();
for (int iItem = 0; iItem < iCount; iItem++)
for (int iCol = 0; iCol < iColSum; iCol++)
{
CString strTemp = m_List.GetItemText( iItem, iCol); if (iCol == 0)
lstrcat(strTemp.GetBuffer(strTemp.GetLength()), "\t");
else
lstrcat(strTemp.GetBuffer(strTemp.GetLength()), "\n"); myFile.WriteString(strTemp.GetBuffer(strTemp.GetLength())); }
//是否立即查看该文件
if (MessageBox("已保存,要查看吗", "完成", MB_YESNO) == IDYES)
{
ShellExecute(NULL,
"open",
pszFileName,
NULL,
NULL,
SW_SHOWNORMAL);
} //关闭文件
myFile.Close();
}