在文件尾追加数据

MFC在文件尾追加数据:

BOOL CDelDlg::WritetoFile(CString sValue)
{
	CString sFile = GetExePath() + "\\1.log";

	CStdioFile file;
	if(file.Open(sFile, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate))
	{
		file.SeekToEnd();				// 移动文件指针到末尾
		file.WriteString(sValue);
		file.Close();
	}
	return FALSE;
}

其中,GetExePath()代码如下:

// 返回可执行文件所在的目录(不包含最后的'\')
CString GetExePath()
{
	char sFileName[256] = {0};
	CString sPath = _T("");
	
	GetModuleFileName(AfxGetInstanceHandle(), sFileName, 255);
	sPath.Format("%s", sFileName);
	int pos = sPath.ReverseFind('\\');
	if(pos != -1)
		sPath = sPath.Left(pos);
	else
		sPath = _T("");
	
	return sPath;
}
原文地址:https://www.cnblogs.com/joeblackzqq/p/1929409.html