MFC文件的保存、打开

调用系统的文件打开、保存对话框

SaveOrOpen.h

#define _CRT_SECURE_NO_WARNINGS
#pragma once

#include"stdafx.h"
#include<afx.h>
#include<afxdlgs.h>

#define POSTFIX _T("文本(*.txt)|*.txt|所有文件(*.*)|*.*||")   //打开、保存文件时的缺省类型
#define _DEFAULT_ _T(".txt")                     //缺省类型

//nType
#define ANSI      0x01
#define _UNICODE  0x02
#define UTF8      0x03
#define UTF16     0x04

class SaveOrOpen
{
private:
	CFile m_file;
	CString m_Path;
	CString OpenANSI(CFile & file);
	void SaveANSI(CFile &file, CString szStr);
public:
	SaveOrOpen();
	~SaveOrOpen();
public:
	CString Open(LPCTSTR szPath);
	void Save(LPCTSTR szPath, CString szStr, int nType = ANSI);
public:
	//调用系统的对话框
	//文件打开对话框,获取对应的路径
	UINT OpenFileDialog(TCHAR *Default = _DEFAULT_, TCHAR *Postfix = POSTFIX);
	//文件保存对话框,获取保存文件的路径
	UINT SaveFileDialog(TCHAR *Default = _DEFAULT_, TCHAR *Postfix = POSTFIX);
	CString GetPath()const;
	CString GetFileName();
private:
	//Unicode转成CHAR* 类型的字符串
	void WideToStr(LPCOLESTR wcstr, char *chResult);
};

SaveOrOpen.cpp

#include "stdafx.h"
#include "SaveOrOpen.h"

#define w CFile::modeCreate|CFile::modeReadWrite
#define r CFile::modeRead
#define h CFile::begin

SaveOrOpen::SaveOrOpen()
{
}


SaveOrOpen::~SaveOrOpen()
{
    m_file.Close();
}

UINT SaveOrOpen::OpenFileDialog(TCHAR *Default, TCHAR *Postfix)
{
	//TRUE是打开文件   Default是缺省   Postfix可选的后缀名
	CFileDialog dlg(TRUE, Default, NULL, 0, Postfix);
	INT_PTR n = dlg.DoModal();
	if (n == IDCANCEL)
		return IDCANCEL;
	m_Path = dlg.GetPathName();
	return IDOK;
}

UINT SaveOrOpen::SaveFileDialog(TCHAR * Default, TCHAR * Postfix)
{
	//FALSE是文件保存  Default是缺省的后缀名  Postfix是可选的后缀名
	CFileDialog dlg(FALSE, Default, NULL, 0, Postfix);
	INT_PTR n = dlg.DoModal();
	if (n == IDCANCEL)
		return IDCANCEL;
	m_Path = dlg.GetPathName();
	return IDOK;
}

CString SaveOrOpen::GetPath() const
{
	return m_Path;
}

CString SaveOrOpen::GetFileName()
{
	return m_file.GetFileName();
}

CString SaveOrOpen::Open(LPCTSTR szPath)
{
	m_file.Open(szPath, r);
	TCHAR s;
	m_file.Read(&s, sizeof(s));
	switch (s)
	{
	case 0xFEFF:
		break;
	default:
		return OpenANSI(m_file);
		break;
	}
}

CString SaveOrOpen::OpenANSI(CFile & file)
{
	ULONGLONG fileSize = file.GetLength();
	CHAR *szStr = new CHAR[fileSize + 1];
	CString Str;

	m_file.Seek(0, h);

	int n = file.Read(szStr, fileSize);
	szStr[n] = 0;

	Str = szStr;
	return Str;
}

//保存
void SaveOrOpen::Save(LPCTSTR szPath, CString szStr, int nType)
{
	m_file.Open(szPath, w);
	switch (nType)
	{
	case ANSI:
		SaveANSI(m_file, szStr);
		break;
	case _UNICODE:
		break;
	case UTF8:
		break;
	case UTF16:
		break;
	default:
		break;
	}
}

void SaveOrOpen::SaveANSI(CFile & file, CString szStr)
{
	int nLen = szStr.GetLength();
	int n = (nLen * 2) + 1;
	char *Buff = new char[n];
	WideToStr(szStr, Buff);
	file.Write(Buff, n);
	delete []Buff;
}

void SaveOrOpen::WideToStr(LPCOLESTR wcstr,char *chResult)
{
	size_t wLen = wcslen(wcstr) + 1;  // 宽字符字符长度,+1表示包含字符串结束符
	int aLen = WideCharToMultiByte(CP_ACP, 0, wcstr, wLen, NULL, 0, NULL, NULL); //第六个参数为0,返回转换所需的char型字符数

	LPSTR lpa = new char[aLen];	//申请缓冲区
	int n = WideCharToMultiByte(CP_ACP, 0, wcstr, wLen, lpa, aLen, NULL, NULL);	//将UNICODE字符集转换到char型缓冲区
	strcpy(chResult, lpa);//内存拷贝
	delete[] lpa;		//释放缓冲区内存
	lpa = NULL;		//缓冲区内存指针设置为null
}

猜你喜欢

转载自blog.csdn.net/qq_38611124/article/details/82760622