檔案寫入讀取和寬字元多位元組字元間轉換

來源:互聯網
上載者:User

// SystemFileControl.h: interface for the CSystemFileControl class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SYSTEMFILECONTROL_H__2B1F6337_5FC2_4CC5_A8B2_B1164D0D38D2__INCLUDED_)
#define AFX_SYSTEMFILECONTROL_H__2B1F6337_5FC2_4CC5_A8B2_B1164D0D38D2__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CSystemFileControl 
{
public:
 CFile fp;
 int file_open_type;

 CSystemFileControl();
 virtual ~CSystemFileControl();
public:
 void OpenFile(int openType);
 void CloseFile(void);
 //*********************系統資訊檔案結構設定函數*********************//
 void SetSYSFILEFlag();//設定資訊檔的檔案頭
 void SetHospitalName(CString str);//設定醫院名稱
 void SetHospitalAddress(CString str);//設定醫院地址
 void SetHospitalPhone(CString str);//設定醫院電話
 //================系統資訊檔案資訊相關讀取函數======================//
 int GetSYSFILEFlag(void *lpBuf);//獲得系統資訊檔案的檔案頭資訊
 CString GetHospitalName();  //獲得醫院名稱
 CString GetHospitalAddress(); //獲得醫院地址
 CString GetHospitalPhone();    //獲得醫院電話
private:
 void CreateNewFile();
};

#endif // !defined(AFX_SYSTEMFILECONTROL_H__2B1F6337_5FC2_4CC5_A8B2_B1164D0D38D2__INCLUDED_)

 

 

 

 

 

// SystemFileControl.cpp: implementation of the CSystemFileControl class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Tele-cardiogram.h"
#include "SystemFileControl.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define File_Create_Write   0
#define File_Write_Read  1
#define File_Only_Write  2
#define File_Only_Read  3
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSystemFileControl::CSystemFileControl()
{
 file_open_type=0;
}

CSystemFileControl::~CSystemFileControl()
{

}

void CSystemFileControl::OpenFile(int OpenType)
{
 BOOL bOk=TRUE;
 CString filename;
 CString str;
 filename="D:\\GY-TELEECG\\SYSFILE.sys";//
 
 switch(OpenType)
 {
 case 1:
  bOk=fp.Open(filename,CFile::modeCreate|CFile::modeWrite);
  file_open_type=File_Create_Write;
  if(bOk)
   SetSYSFILEFlag();
  break;
 case 2:
  bOk=fp.Open(filename,CFile::modeReadWrite);
  file_open_type=File_Write_Read;
  break;
 case 3:
  bOk=fp.Open(filename,CFile::modeWrite);
  file_open_type=File_Only_Write;
  break;
 case 4:
  bOk=fp.Open(filename,CFile::modeRead);
  file_open_type=File_Only_Read;
  break;
 default:
  str=_T("File Doesn't Open!!!");
  str=_T("SYSFILE.sys ")+str;
  AfxMessageBox(str,MB_OK,0);
  return;
  break;
 }
 if(!bOk)
 {
  if(OpenType==1)
  {
   str=_T("請在工作目錄下運行本應用程式!");
   AfxMessageBox(str,MB_OK,0);
   exit(0);
  }
  CreateNewFile();
  exit(0);
 }
}
void CSystemFileControl::CloseFile()
{
 fp.Close();
}
//****************************系統資訊檔案結構設定函數*********************//
void CSystemFileControl::SetSYSFILEFlag()//設定資訊檔的檔案頭
{
 int FileHead_Zero[32];//64位元組
 fp.Seek(0,CFile::begin);
 fp.Write(FileHead_Zero,128);//字  
 fp.Write("SYSTEM",6);
 fp.Write(FileHead_Zero,2);
}
void CSystemFileControl::CreateNewFile()
{
 CString str;
 //int length;

 str=_T("File Can't Open!!!");
 str=_T("SYSFILE.sys")+str+_T("\n系統將嘗試自動修複!");
 AfxMessageBox(str,MB_OK,0);
 
 OpenFile(1);
 str=_T("");

 SetHospitalName(str);
 SetHospitalAddress(str);
 SetHospitalPhone(str);

 fp.SetLength(500);

 CloseFile();
}

void CSystemFileControl::SetHospitalName(CString strHospitalName)//設定醫院名稱
{  
 /***********************************
 寬字元轉換成多位元組字元,存取漢字
 ***********************************/

 //擷取字串在多位元組下的長度
 int dwNum = WideCharToMultiByte(CP_OEMCP,NULL,strHospitalName,-1,NULL,0,NULL,FALSE);
 char *psNameText;//多位元組字串buffer
 psNameText = new char[dwNum];
 if(!psNameText)
 {
  delete []psNameText;
  exit(0);
 }
 //寬字元轉換成多位元組字元
 WideCharToMultiByte (CP_OEMCP,NULL,strHospitalName,-1,psNameText,dwNum,NULL,FALSE);

 fp.Seek(160,CFile::begin);//定位檔案,確定寫入位置
 fp.Write(&dwNum,4);   //寫入醫院名稱在多位元組下的大小
 fp.Write(psNameText,dwNum); //寫入醫院名稱

 delete []psNameText;

}
void CSystemFileControl::SetHospitalAddress(CString strHospitalAddr)//設定醫院地址
{
 fp.Seek(224,CFile::begin);//定位檔案,確定寫入位置
 /***********************************
 寬字元轉換成多位元組字元,存取漢字
 ***********************************/

 //擷取字串在多位元組下的長度
 DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,strHospitalAddr,-1,NULL,0,NULL,FALSE);
 char *psNameText;//多位元組字串buffer
 psNameText = new char[dwNum];
 if(!psNameText)
 {
  delete []psNameText;
  exit(0);
 }
 //寬字元轉換成多位元組字元
 WideCharToMultiByte (CP_OEMCP,NULL,strHospitalAddr,-1,psNameText,dwNum,NULL,FALSE);

 fp.Write(&dwNum,4);   //寫入醫院名稱在多位元組下的大小
 fp.Write(psNameText,dwNum); //寫入醫院名稱

 delete []psNameText;
}
void CSystemFileControl::SetHospitalPhone(CString strHospitalPhone)//設定醫院電話
{
 fp.Seek(384,CFile::begin);//定位檔案,確定寫入位置
 /***********************************
 寬字元轉換成多位元組字元,存取漢字
 ***********************************/

 //擷取字串在多位元組下的長度
 DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,strHospitalPhone,-1,NULL,0,NULL,FALSE);
 char *psNameText;//多位元組字串buffer
 psNameText = new char[dwNum];
 if(!psNameText)
 {
  delete []psNameText;
  exit(0);
 }
 //寬字元轉換成多位元組字元
 WideCharToMultiByte (CP_OEMCP,NULL,strHospitalPhone,-1,psNameText,dwNum,NULL,FALSE);

 fp.Write(&dwNum,4);   //寫入醫院名稱在多位元組下的大小
 fp.Write(psNameText,dwNum); //寫入醫院名稱

 fp.SetLength(500);
 delete []psNameText;
}

int CSystemFileControl::GetSYSFILEFlag(void *lpBuf)//獲得系統資訊檔案的檔案頭資訊
{
 int nCount;
 fp.Seek(128,CFile::begin);
 nCount=fp.Read(lpBuf,5);
 return  nCount;
}
CString CSystemFileControl::GetHospitalName()//獲得醫院名稱及其名稱長度
{
 int nCount=0;//醫院名稱在多位元組下的長度
 /***********************************************************/
 /*          檔案總長度,應該為500,否則檔案讀取出錯        */  
 /***********************************************************/
 int FileSize;
 FileSize=fp.GetLength();
 if(FileSize!=500)
 {
  CloseFile();
  CreateNewFile();
  exit(0);
 }
 /****************************************/
 /*              讀取檔案     */                                             
 /*****************************************/
 fp.Seek(160,CFile::begin);
 fp.Read(&nCount,4);
 char *cTxt;
 cTxt= new char[nCount];
 fp.Read(cTxt,nCount);

 /****************************************/
 /*     多位元組到寬位元組的轉換     */                                             
 /*****************************************/
 DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, cTxt, -1, NULL, 0);//寬位元組下的醫院名稱的長度
 wchar_t *pwNameText;
 pwNameText = new wchar_t[dwNum];
 if(!pwNameText)
 {
  delete []pwNameText;
 }
 MultiByteToWideChar (CP_ACP, 0, cTxt, -1,pwNameText , dwNum);

 /****************************************/
 /*     返回寬位元組下的醫院名稱    */                                             
 /*****************************************/ 
 CString strHospitalName=pwNameText;
 delete []cTxt;
 delete []pwNameText;
 return  strHospitalName;

}

CString CSystemFileControl::GetHospitalAddress()//獲得醫院地址
{
 int nCount=0;//醫院地址在多位元組下的長度
 /***********************************************************/
 /*          檔案總長度,應該為500,否則檔案讀取出錯        */  
 /***********************************************************/
 int FileSize;
 FileSize=fp.GetLength();
 if(FileSize!=500)
 {
  CloseFile();
  CreateNewFile();
  exit(0);
 }
 /****************************************/
 /*              讀取檔案     */                                             
 /*****************************************/
 fp.Seek(224,CFile::begin);
 fp.Read(&nCount,4);
 char *cTxt;
 cTxt= new char[nCount];
 fp.Read(cTxt,nCount);

 /****************************************/
 /*     多位元組到寬位元組的轉換     */                                             
 /*****************************************/
 DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, cTxt, -1, NULL, 0);//寬位元組下的醫院地址的長度
 wchar_t *pwNameText;
 pwNameText = new wchar_t[dwNum];
 if(!pwNameText)
 {
  delete []pwNameText;
 }
 MultiByteToWideChar (CP_ACP, 0, cTxt, -1,pwNameText , dwNum);

 /****************************************/
 /*     返回寬位元組下的醫院地址    */                                             
 /*****************************************/ 
 CString strHospitalAddr=pwNameText;
 delete []cTxt;
 delete []pwNameText;
 return  strHospitalAddr;
}
CString CSystemFileControl::GetHospitalPhone()//獲得醫院電話
{
 int nCount=0;//醫院電話在多位元組下的長度
 /***********************************************************/
 /*          檔案總長度,應該為500,否則檔案讀取出錯        */  
 /***********************************************************/
 int FileSize;
 FileSize=fp.GetLength();
 if(FileSize!=500)
 {
  CloseFile();
  CreateNewFile();
  exit(0);
 }
 /****************************************/
 /*              讀取檔案     */                                             
 /*****************************************/
 fp.Seek(384,CFile::begin);
 fp.Read(&nCount,4);
 char *cTxt;
 cTxt= new char[nCount];
 fp.Read(cTxt,nCount);

 /****************************************/
 /*     多位元組到寬位元組的轉換     */                                             
 /*****************************************/
 DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, cTxt, -1, NULL, 0);//寬位元組下的醫院電話的長度
 wchar_t *pwNameText;
 pwNameText = new wchar_t[dwNum];
 if(!pwNameText)
 {
  delete []pwNameText;
 }
 MultiByteToWideChar (CP_ACP, 0, cTxt, -1,pwNameText , dwNum);

 /****************************************/
 /*     返回寬位元組下的醫院電話    */                                             
 /*****************************************/ 
 CString strHospitalPhone=pwNameText;
 delete []cTxt;
 delete []pwNameText;
 return  strHospitalPhone;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.