📄 ex070101dlg.cpp
字号:
// Ex070101Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "Ex070101.h"
#include "Ex070101Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEx070101Dlg dialog
CEx070101Dlg::CEx070101Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CEx070101Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEx070101Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEx070101Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEx070101Dlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEx070101Dlg, CDialog)
//{{AFX_MSG_MAP(CEx070101Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_API_WRITE, OnApiWrite)
ON_BN_CLICKED(IDC_API_READ, OnApiRead)
ON_BN_CLICKED(IDC_C_WRITE, OnCWrite)
ON_BN_CLICKED(IDC_MFC_READWRITE, OnMfcReadwrite)
ON_BN_CLICKED(IDC_C_READ, OnCRead)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEx070101Dlg message handlers
BOOL CEx070101Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CEx070101Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CEx070101Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CEx070101Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
class CUser : public CObject//记录用户信息
{
DECLARE_SERIAL(CUser)
public:
/*virtual*/ void Serialize( CArchive& ar );
char m_szName[100] ;//用户名
int m_iAge ; //年龄
CUser& operator =(const CUser &other) ;
CUser(char* szName = "",int iAge = 0 );
};
IMPLEMENT_SERIAL(CUser,CObject,1)
CUser::CUser(char *szName, int iAge)
{
if(strlen(szName) > 99 )
ASSERT(false);
memset(m_szName,0,sizeof(m_szName)/sizeof(m_szName[0]));
strcpy(m_szName,szName);
m_iAge = iAge ;
}
CUser& CUser::operator =(const CUser &other)
{
memset(m_szName,0,sizeof(m_szName)/sizeof(m_szName[0]));
strcpy(m_szName,other.m_szName);
m_iAge = other.m_iAge ;
return *this ;
}
//系列化函数
void CUser::Serialize(CArchive &ar)
{
CObject::Serialize(ar);
if(ar.IsStoring())
{
for(int i = 0 ; i < sizeof(m_szName)/sizeof(m_szName[0]) ; i++ )
ar << m_szName[i] ;
ar << m_iAge ;
}
else
{
for(int i = 0 ; i < sizeof(m_szName)/sizeof(m_szName[0]) ; i++ )
ar >> m_szName[i] ;
ar >> m_iAge ;
}
}
void CEx070101Dlg::OnApiWrite()
{
char* pszTxtFileName = "d:\\txt.txt" ;
char* pszAscllFileName = "d:\\ascll.txt" ;
DWORD dNumOfAccess ;
{//API方式存文本文件
CUser user("张三",15);
//打开(新建或打开文件)
HANDLE hFile = ::CreateFile(pszTxtFileName,GENERIC_WRITE,FILE_SHARE_READ,NULL
,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
//对数据处理后写文件
CString strTmp;
strTmp.Format("%s\r\n%d",user.m_szName,user.m_iAge);
::WriteFile(hFile,strTmp,strTmp.GetLength() ,&dNumOfAccess,NULL);
//关闭文件
::CloseHandle(hFile);
}
{//API方式存二进制文件
CUser user("李四",16);
//打开(新建或打开文件)
HANDLE hFile = ::CreateFile(pszAscllFileName,GENERIC_WRITE,FILE_SHARE_READ,NULL
,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
//对数据处理后写文件
DWORD dNumOfAccess ;
::WriteFile(hFile,&user,sizeof(user),&dNumOfAccess,NULL);
//关闭文件
::CloseHandle(hFile);
}
}
void CEx070101Dlg::OnApiRead()
{
char* pszTxtFileName = "d:\\txt.txt" ;
char* pszAscllFileName = "d:\\ascll.txt" ;
DWORD dNumOfAccess ;
{//读文本文件
HANDLE hFile = ::CreateFile(pszTxtFileName,GENERIC_READ,FILE_SHARE_READ,NULL
,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
//取得文件长度
int nLeng = ::SetFilePointer(hFile,0,NULL,FILE_END);
::SetFilePointer(hFile,0,NULL,FILE_BEGIN);
//将文件的内容全部读出来
char *pData = new char[nLeng+1] ;
memset(pData,0,nLeng+1);
::ReadFile(hFile,pData,nLeng,&dNumOfAccess,NULL);
//解析字符串
char szName[100] ;
int iAge ;
sscanf(pData,"%s\r\n%d",szName,&iAge);
//为了便于调试,在下一行加断点,查看szName和iAge的值
delete [] pData ;
CloseHandle(hFile);
}
{
CUser user("",0) ;
HANDLE hFile = ::CreateFile(pszAscllFileName,GENERIC_READ,FILE_SHARE_READ,NULL
,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
//取得文件长度
int nLeng = ::SetFilePointer(hFile,0,NULL,FILE_END);
::SetFilePointer(hFile,0,NULL,FILE_BEGIN);
//读取内容
char *pData = new char[nLeng+1] ;
memset(pData,0,nLeng+1);
::ReadFile(hFile,pData,nLeng,&dNumOfAccess,NULL);
user = *(CUser*)pData ;
//为了便于调试,在下一行加断点,查看user的值
delete [] pData ;
CloseHandle(hFile);
}
}
void CEx070101Dlg::OnCWrite()
{
char* pszTxtFileName = "d:\\txt.txt" ;
char* pszAscllFileName = "d:\\ascll.txt" ;
{//文本方式写
CUser user("张三",15) ;
FILE * pFile = fopen(pszTxtFileName,"wt");
fprintf(pFile,"%s\r\n%d",user.m_szName,user.m_iAge);
fclose(pFile);
}
{//二进制方式写
CUser user("李四",16) ;
FILE * pFile = fopen(pszAscllFileName,"wb");
fwrite(&user,sizeof(user),1,pFile);
fclose(pFile);
}
}
void CEx070101Dlg::OnCRead()
{
char* pszTxtFileName = "d:\\txt.txt" ;
char* pszAscllFileName = "d:\\ascll.txt" ;
{//文本方式读
CUser user("",0);
FILE * pFile = fopen(pszTxtFileName,"rt");
fscanf(pFile,"%s\r\n%d",user.m_szName,&user.m_iAge);
fclose(pFile);
}
{//二进制方式读
CUser user("",0);
FILE * pFile = fopen(pszAscllFileName,"rb");
fread(&user,sizeof(user),1,pFile);
fclose(pFile);
}
}
void CEx070101Dlg::OnMfcReadwrite()
{
char* pszAscllFileName = "d:\\ascll.txt" ;
{
CUser user("张三",15) ;
CFile file ;
file.Open(pszAscllFileName,CFile::modeCreate|CFile::modeWrite);
CArchive arFile(&file,CArchive::store) ;
user.Serialize(arFile) ;
//此处加断点,查看user的新值
arFile.Close();
file.Close();
}
{
CUser user ;
CFile file ;
file.Open(pszAscllFileName,CFile::modeRead);
CArchive arFile(&file,CArchive::load) ;
user.Serialize(arFile) ;
//此处加断点,查看user的新值
arFile.Close();
file.Close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -