// OperTxtFile.cpp: implementation of the COperTxtFile class. // ////////////////////////////////////////////////////////////////////// #include "geomative.h" #include "OperTxtFile.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif extern int g_iUILanguage; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// COperTxtFile::COperTxtFile() { m_uiParamWidth = 18; m_pFile = NULL; } COperTxtFile::~COperTxtFile() { if (m_pFile) { fclose(m_pFile); m_pFile = NULL; } } void COperTxtFile::SetParamWidth(UINT uiWidth) { if (uiWidth < 6 || uiWidth >50) { CString str = _T(""); if (LANG_ZHCN == g_iUILanguage) { str.Format(_T("设置参数宽度错误!值为%d"), uiWidth); AfxMessageBox(str); } else { str.Format(_T("Set Parameter Width error!value = %d"), uiWidth); MessageBoxEx(NULL, str, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } return; } m_uiParamWidth = uiWidth; } void COperTxtFile::CloseFile() { if (m_pFile) { fclose(m_pFile); m_pFile = NULL; } } bool COperTxtFile::OpenFileforWrite(CString strFileName) { if (strFileName.IsEmpty()) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("文件名不能为空")); else MessageBoxEx(NULL, _T("File's name can not be empty!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } if (m_pFile) { CloseFile(); } m_pFile = fopen(strFileName, "w+"); if (NULL == m_pFile) { CString strShow = _T(""); if (LANG_ZHCN == g_iUILanguage) { strShow.Format(_T("打开文件 %s 失败!错误码 = %d"), strFileName, GetLastError()); AfxMessageBox(strShow); } else { strShow.Format(_T("Open file %s failed!ErrorCode = %d"), strFileName, GetLastError()); MessageBoxEx(NULL, strShow, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } return false; } CFileOperTools::GetInstance()->WriteComLog(_T("zm: COperTxtFile::OpenFileforWrite export file : -------------------") + strFileName); return true; } //按照m_uiParamWidth来写TXT文件,不足的部分以空格来填充 bool COperTxtFile::WriteFileContent(const CStringArray& strArrContent) { CString strText = _T(""); for (int i = 0; i < strArrContent.GetSize(); i++) { int nLen = strArrContent[i].GetLength(); if (nLen > m_uiParamWidth) { if (LANG_ZHCN == g_iUILanguage) { strText.Format(_T("内容长度(%s)(%d)超过参数宽度(%d)!"), strArrContent[i], nLen, m_uiParamWidth); AfxMessageBox(strText); } else { strText.Format(_T("The length of content(%s)(%d) is over than parameter width(%d)!"), strArrContent[i], nLen, m_uiParamWidth); MessageBoxEx(NULL, strText, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } //return false; } strText.Empty(); strText = strArrContent[i]; for (int j = 0; j < (m_uiParamWidth - nLen); j++) { strText += _T(" "); } //在最后一个元素前面加上换行符 if (i == strArrContent.GetSize()-1) { strText += _T("\r\n"); } nLen = fwrite(strText, 1, strText.GetLength(), m_pFile); if (nLen != strText.GetLength()) { CString str = _T(""); if (LANG_ZHCN == g_iUILanguage) { str.Format(_T("写文件错误!应该写长度= %d,写长度=%d, error_code=%d"), strText.GetLength(), nLen, GetLastError()); AfxMessageBox(str); } else { str.Format(_T("Write file error!should write length = %d, writed length = %d, error_code = %d"), strText.GetLength(), nLen, GetLastError()); MessageBoxEx(NULL, str, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } return false; } } fflush(m_pFile); return true; } bool COperTxtFile::WriteEmptyRow() { CString strText = _T("\r\n"); fwrite(strText, 1, strText.GetLength(), m_pFile); fflush(m_pFile); return true; }