向已存在的文件中插入首行
BOOL AddTitleColumn(CString &sTxtFile)
{
CStdioFile stdFile;
if ( stdFile.Open(sTxtFile, CFile::modeCreate | CFile::modeNoTruncate
| CFile::modeReadWrite|CFile::shareExclusive) )
{
CIni ini(CCommonFun::GetExecutablePath() +"Config.ini");
CString sTitleRow = ini.GetString("ListCtrl", "ViewColumName");
CStringArray arrLines;
arrLines.Add(sTitleRow);
stdFile.SeekToBegin();
CString str;
while(stdFile.ReadString(str))
{
arrLines.Add(str);
}
stdFile.SeekToBegin();
int nCount = arrLines.GetSize();
for ( int i = 0; i < nCount; i++ )
{
CString sContent;
if (i > 0)
{
sContent.Format("%d,%s", i, arrLines[i]);
}
else
{
sContent = arrLines[i];
}
stdFile.WriteString (sContent);
stdFile.WriteString ("\n");
}
stdFile.Close();
return TRUE;
}
return FALSE;
}
生成一个带标题的文件
BOOL CBarView::ExportDataFile(CString sDataFile/*, CStringArray &arsWriteString*/)
{
CStdioFile stdFile;
if ( stdFile.Open(sDataFile, CFile::modeCreate | CFile::modeNoTruncate
| CFile::modeReadWrite|CFile::shareExclusive) )
{
CString sTitleRow = "序号,文件名,份数,页数";
stdFile.WriteString (sTitleRow);
stdFile.WriteString ("\n");
for (int i = 0; i < (int)m_data.data.size(); i++)
{
CString sLineData;
for (int j = 0; j < (int)m_data.data.at(i).size(); j++)
{
CString sData = m_data.data.at(i).at(j);
if (j != 0)
{
sData.Format(", %s", sData);
}
else
{
sData.Format("%s", sData);
}
sLineData += sData;
}
CString sLine;
sLine.Format("%d, %s", (i+1), sLineData);
stdFile.WriteString (sLine);
stdFile.WriteString ("\n");
}
stdFile.Close();
return TRUE;
}
LOG("创建文件 %s 失败", sDataFile);
return FALSE;
}