BOOL CCustomAutoComplete::SaveList(LPCTSTR pszFileName) { FILE* fp = _tfsopen(pszFileName, _T("wb"), _SH_DENYWR); if (fp == NULL) return FALSE; // write Unicode byte-order mark 0xFEFF fputwc(0xFEFF, fp); for (int i = 0; i < m_asList.GetCount(); i++) _ftprintf(fp, _T("%s\r\n"), (LPCTSTR)m_asList[i]); fclose(fp); return !ferror(fp); }
Leaving exception topics aside, no checking of returned values is done too.
Here is concise version of new code:
BOOL CCustomAutoComplete::SaveList(LPCTSTR pszFileName) { FILE* fp = _tfsopen(pszFileName, _T("wb"), _SH_DENYWR); if (fp == NULL) return FALSE; BOOL ret = (fputwc(0xFEFF, fp) != WEOF); // write Unicode byte-order mark 0xFEFF for (int i = 0; ret && i < m_asList.GetCount(); i++) ret = (_ftprintf(fp, _T("%s\r\n"), (LPCTSTR)m_asList[i]) > 0); return fclose(fp) ? FALSE : ret; }
This post has been edited by fox88: 04 May 2016 - 06:19 PM