⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileopradlg.cpp

📁 读写文件操作实例 EVC 4.0 ARMV4I 编译通过 运行成功
💻 CPP
字号:
// FileOpraDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FileOpra.h"
#include "FileOpraDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFileOpraDlg dialog

CFileOpraDlg::CFileOpraDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFileOpraDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFileOpraDlg)
	m_strDisp = _T("");
	m_strFileName = _T("");
	m_strWrite = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFileOpraDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileOpraDlg)
	DDX_Text(pDX, IDC_EDIT_DISP, m_strDisp);
	DDX_Text(pDX, IDC_EDIT_FILENAME, m_strFileName);
	DDX_Text(pDX, IDC_EDIT_WRITE, m_strWrite);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFileOpraDlg, CDialog)
	//{{AFX_MSG_MAP(CFileOpraDlg)
	ON_BN_CLICKED(IDC_CREATE_FILE, OnCreateFile)
	ON_BN_CLICKED(IDC_DELETE_FILE, OnDeleteFile)
	ON_BN_CLICKED(IDC_CLEAR_DISP, OnClearDisp)
	ON_BN_CLICKED(IDC_WRITE_FILE, OnWriteFile)
	ON_BN_CLICKED(IDC_READ_FILE, OnReadFile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileOpraDlg message handlers

HANDLE hFile = INVALID_HANDLE_VALUE;                                    /*  文件句柄                    */
BOOL CFileOpraDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	m_strFileName="test.txt";                                           /*  默认的文件名                */
	UpdateData(FALSE);                                                  /*  更新编辑框显示              */
	return TRUE;  // return TRUE  unless you set the focus to a control
}

/*********************************************************************************************************
** Function name:		OnCreateFile
** Descriptions:		"创建/打开文件"按键单击事件代码
** input parameters:	无
** output parameters:	无
** Returned value:		无
*********************************************************************************************************/
void CFileOpraDlg::OnCreateFile() 
{
	// TODO: Add your control notification handler code here
	CString pStrFileName="";

	UpdateData(TRUE);

	if (m_strFileName == "") {                                          /*  判断文件是否输入文件名      */
		MessageBox(_T("请输入要创建或打开文件的文件名!"));
		return;
	}
	
	pStrFileName = _T("\\FlashDisk\\") + m_strFileName;                /*  取得文件名及路径            */
	
	if(hFile != INVALID_HANDLE_VALUE) {                                 /*  在创建/打开其它文件前       */
		CloseHandle(hFile);                                             /*  先关闭当前文件              */
	}                                                                   
	hFile = CreateFile(pStrFileName, GENERIC_READ | GENERIC_WRITE, 0,   /*  打开一个已有的文件,         */
					   NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); /*  若该文件不存在,则创建该文件 */
	if (hFile == INVALID_HANDLE_VALUE) {                                /*  打开文件失败                */
		MessageBox(_T("打开/创建文件失败!"),_T("Error"),MB_OK|MB_ICONERROR);
		return;
	}

	MessageBox(_T("成功打开/创建文件:") + m_strFileName);
}

/*********************************************************************************************************
** Function name:		OnDeleteFile
** Descriptions:		"删除文件"按键单击事件代码
** input parameters:	无
** output parameters:	无
** Returned value:		无
*********************************************************************************************************/
void CFileOpraDlg::OnDeleteFile()	
{
	// TODO: Add your control notification handler code here
	CString pStrFileName="";
	
	UpdateData(TRUE);

	if (m_strFileName == "") {                                          /*  判断是否输入文件名          */
		MessageBox(_T("请输入要删除文件的文件名!"));
		return;
	}

	pStrFileName = _T("\\FlashDisk\\") + m_strFileName;                /*  取得文件名及路径            */
	if(hFile != INVALID_HANDLE_VALUE) {								
		CloseHandle(hFile);												/*  若文件已打开,则先关闭该文件 */
		hFile = INVALID_HANDLE_VALUE;
	}
	BOOL bRet = DeleteFile(pStrFileName);                               /*  删除文件                    */
	if (bRet == FALSE) {                                                
		MessageBox(_T("文件删除失败!\r\n该文件可能不存在"),_T("ERROR"),MB_ICONERROR|MB_OK);
	} else {
		MessageBox(_T("文件删除成功"),MB_OK);
	}
}

/*********************************************************************************************************
** Function name:		OnWriteFile
** Descriptions:		"写"按键单击事件代码
** input parameters:	无
** output parameters:	无
** Returned value:		无
*********************************************************************************************************/
void CFileOpraDlg::OnWriteFile() 
{
	// TODO: Add your control notification handler code here
	DWORD dwLen,dwActLen;
	char *pcBuff = NULL;

	if (hFile == INVALID_HANDLE_VALUE) {
		MessageBox(_T("文件未打开!"),_T("Error"),MB_OK|MB_ICONERROR);
		return;	
	}

	UpdateData(TRUE);

	dwLen = m_strWrite.GetLength();                                     /*  取得输入字符串的长度        */

	pcBuff = new char[dwLen];                                           /*  开辟一段缓冲区,供转换编码用 */
	if (pcBuff == NULL) {
		MessageBox(_T("写文件失败!\r\n原因: 开辟缓冲区失败!"),_T("ERROR"),MB_ICONERROR|MB_OK);
		return;
	}

	LPTSTR lpszStr = m_strWrite.GetBuffer(dwLen);                       /*  取得输入字符串的缓冲区指针  */

	int iCount = WideCharToMultiByte(CP_ACP, 0, lpszStr, dwLen, pcBuff, dwLen, NULL, NULL);
	                                                                    /*  将 Unicode 的 CString 转换  */
	                                                                    /*  为 char 型                  */
	m_strWrite.ReleaseBuffer();                                         /*  释放缓冲区                  */
	if (iCount == 0) {													/*  Unicode 转 char 失败        */
		MessageBox(_T("WideCharToMultiByte failed!"),_T("Error"),MB_OK|MB_ICONERROR);
		delete[] pcBuff;
		return;
	}

	BOOL bRet = SetFilePointer(hFile, 0, NULL, FILE_END);               /*  移动文件指针到文件末尾      */
	if (bRet == 0xFFFFFFFF) {
		MessageBox(_T("将文件指针移至文件末尾失败!"),_T("Error"),MB_OK|MB_ICONERROR);
		delete[] pcBuff;
		return;	
	}

	bRet = WriteFile(hFile, pcBuff, dwLen, &dwActLen, NULL);            /*  将数据写入文件中            */
	if (bRet == TRUE) {
		MessageBox(_T("写文件成功!"));	
	} else {
		MessageBox(_T("写文件失败!"),_T("Error"),MB_OK|MB_ICONERROR);	
	}
	
	delete[] pcBuff;
}

/*********************************************************************************************************
** Function name:		OnReadFile
** Descriptions:		"读"按键单击事件代码
** input parameters:	无
** output parameters:	无
** Returned value:		无
*********************************************************************************************************/
void CFileOpraDlg::OnReadFile() 
{
	// TODO: Add your control notification handler code here
	DWORD dwFileLen,dwActLen;
	char *pcBuff = NULL;

	if (hFile == INVALID_HANDLE_VALUE) {
		MessageBox(_T("文件未打开!"),_T("Error"),MB_OK|MB_ICONERROR);
		return;	
	}
	
	dwFileLen = GetFileSize(hFile, NULL);                               /*  获取文件大小                */
	if (dwFileLen == 0xFFFFFFFF) {
		MessageBox(_T("获取文件大小失败!"),_T("Error"),MB_OK|MB_ICONERROR);
		return;	
	}

	BOOL bRet = SetFilePointer(hFile, 0, NULL, FILE_BEGIN);	            /*  移动文件指针到文件开头      */	
	if (bRet == 0xFFFFFFFF) {
		MessageBox(_T("将文件指针移至文件开头失败!"),_T("Error"),MB_OK|MB_ICONERROR);
		return;	
	}

	pcBuff = new char[dwFileLen+1];
	if (pcBuff == NULL) {
		MessageBox(_T("读文件失败!\r\n原因: 开辟缓冲区失败!"),_T("ERROR"),MB_ICONERROR|MB_OK);
		return;
	}

	bRet = ReadFile(hFile, pcBuff, dwFileLen, &dwActLen, NULL);	        /*  从文件中读出数据            */
	if (bRet == TRUE) {
		LPTSTR lpszStr = m_strDisp.GetBuffer(dwFileLen+1);	
		*(lpszStr+dwFileLen)='\0';                                      /*  在末尾添加结束符 '\0'       */
		int iCount = MultiByteToWideChar(CP_ACP, 0, pcBuff, dwFileLen, lpszStr, dwFileLen);
	                                                                    /*  将 char 型 转换为 Unicode   */
		m_strDisp.ReleaseBuffer();
		if (iCount == 0) {
			MessageBox(_T("MultiByteToWideChar failed!"),_T("Error"),MB_OK|MB_ICONERROR);
			delete[] pcBuff;                                            /*  在返回前先删除 pcBuff       */	
			return;
		}
		UpdateData(FALSE);                                              /*  更新显示编辑框内容          */
		MessageBox(_T("读文件成功!"));	
	} else {
		MessageBox(_T("读文件失败!"),_T("Error"),MB_OK|MB_ICONERROR);	
	}

	delete[] pcBuff;	
}

/*********************************************************************************************************
** Function name:		OnClearDisp
** Descriptions:		"清除显示"按键单击事件代码
** input parameters:	无
** output parameters:	无
** Returned value:		无
*********************************************************************************************************/
void CFileOpraDlg::OnClearDisp() 
{
	// TODO: Add your control notification handler code here
	m_strDisp="";                                                       /*  清除读取显示框显示          */
	m_strWrite="";                                                      /*  清除写入编辑框显示          */
	UpdateData(FALSE);                                                  /*  更新显示                    */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -