MFC保存文本文件的两种方法(包含如何获取工作路径)
来源:互联网 发布:罪恶装备人物乐乎 编辑:程序博客网 时间:2024/06/10 20:48
第一种方法,从网上找到的,直接使用了CFile类,其中用的那些古怪的处理其实是为了存储中文,所以需要添加unicode格式转化。使用比较抽象而且需要添加头文件
#include <atlconv.h>
CString str = "测试储存文件//n";
CString str2 = "附加";
//A2W (LPCSTR) -> (LPWSTR)
USES_CONVERSION;
unsigned short* pstr = A2W((LPCTSTR) str);
int Len = 2 * wcslen(pstr);
unsigned short* pstr2 = A2W((LPCTSTR) str2);
int Len2 = 2 * wcslen(pstr2);
CFile file;
file.Open( "D://123.txt ", CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyWrite, NULL);
BYTE UH[] = {0xff, 0xfe};
file.Write(UH, 2);
file.Write(pstr, Len);
file.Write(pstr2, Len2);
file.Close();
第二种方法,一下截自我写的一段程序,可以直接使用~~
TCHAR sgCurPath[MAX_PATH];
ZeroMemory(sgCurPath, sizeof(sgCurPath));
GetModuleFileName(NULL,sgCurPath,sizeof(sgCurPath)/sizeof(TCHAR));
CString sgModulePath = sgCurPath;
sgModulePath = sgModulePath.Left(sgModulePath.ReverseFind('//'));
sgModulePath = sgModulePath + "//myfile.txt";
char* saveFileName= (LPSTR)(LPCTSTR)sgModulePath;
// 获取 当前程序工作目录~~~~~
CStdioFile myFile;
CFileException fileException;
CString temp_save;
CTime time = CTime::GetCurrentTime();///构造CTime对象
int m_nYear = time.GetYear();///年
int m_nMonth = time.GetMonth();///月
int m_nDay = time.GetDay();///日
int m_nHour = time.GetHour();///小时
int m_nMinute = time.GetMinute();///分钟
int m_nSecond = time.GetSecond();///秒
//获取当前保存时间 写入文件
CString m_strTime = time.Format("%Y-%m-%d %H:%M:%S");
if(myFile.Open(saveFileName,CFile::typeText|CFile::modeCreate|CFile::modeReadWrite| CFile::shareDenyWrite),&fileException)
{
//开始向文本文件中写入myfile,
m_strTime = "当前保存时间: "+ m_strTime + "/n";
myFile.WriteString(m_strTime);
temp_save = " 这是一个测试程序";
myFile.WriteString(temp_save);
}
else
{
TRACE("Can't open file %s,error=%u/n",saveFileName,fileException.m_cause);
}
- MFC保存文本文件的两种方法(包含如何获取工作路径)
- Java获取文本文件字符编码的两种方法
- 两种获取文件路径的方法
- MFC 获取工作路径
- MFC Button Control事件获取的两种方法
- MFC Button Control事件获取的两种方法
- 获取Java文件路径的两种方法
- 获取沙盒文件路径的两种方法
- VC/MFC获取路径的方法
- mfc 获取当前工作路径,获取当前exe绝对路径,设置当前工作路径的函数
- 获取程序工作路径的方法
- VS2010 项目属性的默认包含路径的两种设置方法
- 中关村黑马程序员 --- 复制文本文件的两种方法
- Java 实现复制文本文件的两种方法
- Java中文本文件读取的两种方法
- C#中如何获取当前路径的几种方法
- C#中如何获取当前路径的几种方法
- 图片保存的两种方法
- 正则表达式30分钟入门教程
- windows 2003服务器优化设置
- 漫谈数据库索引
- 何你的简历石沉大海———业内人士惊暴网络求职黑幕!!
- 自定义控件的使用以及与用户控件的区别
- MFC保存文本文件的两种方法(包含如何获取工作路径)
- 什么是谓词
- 移动adc接入的des加密方式以及编码格式
- 回答4个问题帮助你分析:是否已经真正进入到Agile manager的角色?
- 开发学习备录一
- Ajax 实现页面数据 后台验证
- 什么是先天综合命题?
- retrive ip address list change event by EventSelect and Overlapped
- 优化 Windows Server 2003 服务器性能