MFC檔案的儲存、開啟
阿新 • • 發佈:2018-12-10
呼叫系統的檔案開啟、儲存對話方塊
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() { } 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 }