写字符串到文本【可设定格式】
2012-05-05 20:03
162 查看
/******************************************************************************* 函数名称: WorkClass::WriteString2File 写字符串内容到文件 ================================================================================ 参数说明: const CString strText 文件内容 参数说明: const CString strSavePath 文件路径 参数说明: BOOL bUtf_8 是否为UTF-8格式 -------------------------------------------------------------------------------- 返回值: BOOL -------------------------------------------------------------------------------- 文件作者: King.Sollyu QQ:191067617 *******************************************************************************/BOOL WorkClass::WriteString2File( const CString strText,const CString strSavePath,BOOL bUtf_8 ) { CFile fileText; CFileException fileException; if (fileText.Open(strSavePath,CFile::modeReadWrite|CFile::modeCreate,&fileException) == FALSE) { ASSERT (FALSE); // 打开文件错误,识别什么错误 switch (fileException.m_cause) { case CFileException::fileNotFound: return SetLastError(ERR_FILENOTFOUND),FALSE;break; case CFileException::accessDenied: return SetLastError(ERR_ACCESSDENIED),FALSE;break; case CFileException::sharingViolation: return SetLastError(ERR_SHARINGVIOLATION),FALSE;break; default:return SetLastError(ERR_UNKNOW),FALSE;break; } } if (bUtf_8 == TRUE) { unsigned char data[3] = {0xEF, 0xBB, 0xBF}; fileText.Write(data,3); int nSrcLen = (int)wcslen(strText); CStringA utf8String(strText); int nBufLen = (nSrcLen+1) * 6; LPSTR buffer = utf8String.GetBufferSetLength(nBufLen); // 将UNICODE 转换成UTF8 // 需要函数AtlUnicodeToUTF8 头文件: <atlenc.h> int nLen = AtlUnicodeToUTF8(strText, nSrcLen, buffer, nBufLen); buffer[nLen] = 0; utf8String.ReleaseBuffer(); //写文件 fileText.SeekToEnd(); fileText.Write((LPCSTR)utf8String, nLen); }else { CStringA strANSI(strText); fileText.Write((LPCSTR)strANSI,strANSI.GetLength()); } fileText.Close(); return TRUE; }
相关文章推荐
- 读取Html文本字符串时可能遇到输出时打印字体格式或者换行符之类问题的情况
- python 4-3 如何调整字符串中文本格式re.sub(p,r'\1/\2',str1)/re.sub('(?P<Name>p),'\g<Name>',str1)
- 4-3调整字符串中的文本格式
- python知识:json格式文本;异常处理;字符串处理;unicode类型和str类型转换
- 读取文本,匹配固定格式的字符串写入到另一个文件
- 正则批量替换 如何调整字符串中文本的格式
- python_改变字符串中文本格式?
- 字符串渲染 多种格式的文本 UILabel显示不同的格式
- QT操作Excel封装类(包含高级功能:合并单元格,文本及单元格格式设定等)
- vi部分命令. 替换操作; 文本格式转换; 在整个文件中替换; 在行首尾添加字符串.
- 将图像数据保存成文本格式(字符串)
- 使用js设定文本格式
- VC 设置 Excel 的单元格格式 数字形式字符串 文本形式
- jsp输出xml内容的字符串,保证标签输出成文本,保证换行空格缩进格式
- python-13-如何调整字符串中文本的格式?sub函数
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
- C#将字典转换成name=value这种字符串格式
- 字符串 格式对齐
- 解决Oracle异常ORA-01861:literal does not match format string(字符串格式不匹配)
- Spring boot项目设定返回数据为JSON固定格式