Conversion between file write reading and wide-character multi-byte characters

Source: Internet
Author: User

// Systemfilecontrol. h: interface for the csystemfilecontrol class.
//
//////////////////////////////////////// //////////////////////////////

# If! Defined (afx_systemfilecontrol_h1_2b1f6337_5fc2_4cc5_a8b2_b1164d0d38d21_encoded _)
# Define afx_systemfilecontrol_h1_2b1f6337_5fc2_4cc5_a8b2_b1164d0d38d21_encoded _

# 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 );
// ********************* System information file structure setting function *********** **********//
Void setsysfileflag (); // sets the file header of the information file.
Void sethospitalname (cstring Str); // set the hospital name
Void sethospitaladdress (cstring Str); // set the hospital address
Void sethospitalphone (cstring Str); // set the hospital phone number
// ========================= System information file-related read functions ============================== ========== //
Int getsysfileflag (void * lpbuf); // obtain the file header information of the system information file
Cstring gethospitalname (); // obtain the hospital name
Cstring gethospitaladdress (); // obtain the hospital address
Cstring gethospitalphone (); // get the hospital phone number
PRIVATE:
Void createnewfile ();
};

# Endif //! Defined (afx_systemfilecontrol_h1_2b1f6337_5fc2_4cc5_a8b2_b1164d0d38d21_encoded _)

 

 

 

 

 

// 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 ("Run this application in the working directory! ");
Afxmessagebox (STR, mb_ OK, 0 );
Exit (0 );
}
Createnewfile ();
Exit (0 );
}
}
Void csystemfilecontrol: closefile ()
{
FP. Close ();
}
// ***************************** System information file structure setting function **** *****************//
Void csystemfilecontrol: setsysfileflag () // sets the information file header.
{
Int filehead_zero [32]; // 64 bytes
FP. Seek (0, cfile: Begin );
FP. Write (filehead_zero, 128); // word
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 the system will try to fix it automatically! ");
Afxmessagebox (STR, mb_ OK, 0 );
 
Openfile (1 );
STR = _ T ("");

Sethospitalname (STR );
Sethospitaladdress (STR );
Sethospitalphone (STR );

FP. setlength (500 );

Closefile ();
}

Void csystemfilecontrol: sethospitalname (cstring strhospitalname) // set the hospital name
{
/***********************************
Converts wide characters into multi-byte characters to access Chinese Characters
***********************************/

// Obtain the length of a string in multiple bytes
Int dwnum = widechartomultibyte (cp_oemcp, null, strhospitalname,-1, null, 0, null, false );
Char * psnametext; // multi-byte String Buffer
Psnametext = new char [dwnum];
If (! Psnametext)
{
Delete [] psnametext;
Exit (0 );
}
// Convert wide characters into multi-byte characters
Widechartomultibyte (cp_oemcp, null, strhospitalname,-1, psnametext, dwnum, null, false );

FP. Seek (160, cfile: Begin); // locate the file and determine the write location
FP. Write (& dwnum, 4); // the size of the hospital name in multiple bytes
FP. Write (psnametext, dwnum); // write the hospital name

Delete [] psnametext;

}
Void csystemfilecontrol: sethospitaladdress (cstring strhospitaladdr) // set the hospital address
{
FP. Seek (224, cfile: Begin); // locate the file and determine the write location
/***********************************
Converts wide characters into multi-byte characters to access Chinese Characters
***********************************/

// Obtain the length of a string in multiple bytes
DWORD dwnum = widechartomultibyte (cp_oemcp, null, strhospitaladdr,-1, null, 0, null, false );
Char * psnametext; // multi-byte String Buffer
Psnametext = new char [dwnum];
If (! Psnametext)
{
Delete [] psnametext;
Exit (0 );
}
// Convert wide characters into multi-byte characters
Widechartomultibyte (cp_oemcp, null, strhospitaladdr,-1, psnametext, dwnum, null, false );

FP. Write (& dwnum, 4); // the size of the hospital name in multiple bytes
FP. Write (psnametext, dwnum); // write the hospital name

Delete [] psnametext;
}
Void csystemfilecontrol: sethospitalphone (cstring strhospitalphone) // set the hospital phone number
{
FP. Seek (384, cfile: Begin); // locate the file and determine the write location
/***********************************
Converts wide characters into multi-byte characters to access Chinese Characters
***********************************/

// Obtain the length of a string in multiple bytes
DWORD dwnum = widechartomultibyte (cp_oemcp, null, strhospitalphone,-1, null, 0, null, false );
Char * psnametext; // multi-byte String Buffer
Psnametext = new char [dwnum];
If (! Psnametext)
{
Delete [] psnametext;
Exit (0 );
}
// Convert wide characters into multi-byte characters
Widechartomultibyte (cp_oemcp, null, strhospitalphone,-1, psnametext, dwnum, null, false );

FP. Write (& dwnum, 4); // the size of the hospital name in multiple bytes
FP. Write (psnametext, dwnum); // write the hospital name

FP. setlength (500 );
Delete [] psnametext;
}

Int csystemfilecontrol: getsysfileflag (void * lpbuf) // obtain the file header information of the system information file
{
Int ncount;
FP. Seek (128, cfile: Begin );
Ncount = FP. Read (lpbuf, 5 );
Return ncount;
}
Cstring csystemfilecontrol: gethospitalname () // obtain the hospital name and its name length.
{
Int ncount = 0; // The length of the hospital name in multiple bytes
/*************************************** ********************/
/* Total file length, which should be 500; otherwise, an error occurred while reading the file */
/*************************************** ********************/
Int filesize;
Filesize = FP. getlength ();
If (filesize! = 500)
{
Closefile ();
Createnewfile ();
Exit (0 );
}
/*************************************** */
/* Read the file */
/*************************************** **/
FP. Seek (160, cfile: Begin );
FP. Read (& ncount, 4 );
Char * ctxt;
Ctxt = new char [ncount];
FP. Read (ctxt, ncount );

/*************************************** */
/* Conversion from multi-byte to wide byte */
/*************************************** **/
DWORD dwnum = multibytetowidechar (cp_acp, 0, ctxt,-1, null, 0); // The length of the hospital name in the width byte
Wchar_t * pwnametext;
Pwnametext = new wchar_t [dwnum];
If (! Pwnametext)
{
Delete [] pwnametext;
}
Multibytetowidechar (cp_acp, 0, ctxt,-1, pwnametext, dwnum );

/*************************************** */
/* Return the hospital name in the width byte */
/*************************************** **/
Cstring strhospitalname = pwnametext;
Delete [] ctxt;
Delete [] pwnametext;
Return strhospitalname;

}

Cstring csystemfilecontrol: gethospitaladdress () // get the hospital address
{
Int ncount = 0; // The length of the hospital address in multiple bytes
/*************************************** ********************/
/* Total file length, which should be 500; otherwise, an error occurred while reading the file */
/*************************************** ********************/
Int filesize;
Filesize = FP. getlength ();
If (filesize! = 500)
{
Closefile ();
Createnewfile ();
Exit (0 );
}
/*************************************** */
/* Read the file */
/*************************************** **/
FP. Seek (224, cfile: Begin );
FP. Read (& ncount, 4 );
Char * ctxt;
Ctxt = new char [ncount];
FP. Read (ctxt, ncount );

/*************************************** */
/* Conversion from multi-byte to wide byte */
/*************************************** **/
DWORD dwnum = multibytetowidechar (cp_acp, 0, ctxt,-1, null, 0); // The length of the hospital address in the width byte
Wchar_t * pwnametext;
Pwnametext = new wchar_t [dwnum];
If (! Pwnametext)
{
Delete [] pwnametext;
}
Multibytetowidechar (cp_acp, 0, ctxt,-1, pwnametext, dwnum );

/*************************************** */
/* Return the hospital address in the width byte */
/*************************************** **/
Cstring strhospitaladdr = pwnametext;
Delete [] ctxt;
Delete [] pwnametext;
Return strhospitaladdr;
}
Cstring csystemfilecontrol: gethospitalphone () // get the hospital phone number
{
Int ncount = 0; // The length of the hospital phone number in multiple bytes
/*************************************** ********************/
/* Total file length, which should be 500; otherwise, an error occurred while reading the file */
/*************************************** ********************/
Int filesize;
Filesize = FP. getlength ();
If (filesize! = 500)
{
Closefile ();
Createnewfile ();
Exit (0 );
}
/*************************************** */
/* Read the file */
/*************************************** **/
FP. Seek (384, cfile: Begin );
FP. Read (& ncount, 4 );
Char * ctxt;
Ctxt = new char [ncount];
FP. Read (ctxt, ncount );

/*************************************** */
/* Conversion from multi-byte to wide byte */
/*************************************** **/
DWORD dwnum = multibytetowidechar (cp_acp, 0, ctxt,-1, null, 0); // The length of the hospital phone in the width byte
Wchar_t * pwnametext;
Pwnametext = new wchar_t [dwnum];
If (! Pwnametext)
{
Delete [] pwnametext;
}
Multibytetowidechar (cp_acp, 0, ctxt,-1, pwnametext, dwnum );

/*************************************** */
/* Return the hospital phone number in the byte range */
/*************************************** **/
Cstring strhospitalphone = pwnametext;
Delete [] ctxt;
Delete [] pwnametext;
Return strhospitalphone;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.