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

📄 createfiledlg.cpp

📁 大型实验:Unix文件管系统模拟。用内存中的一段区域模拟硬盘空间
💻 CPP
字号:
// CreatefileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FileSys.h"
#include "CreatefileDlg.h"

#include "filsys.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCreatefileDlg dialog
CCreatefileDlg::CCreatefileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCreatefileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCreatefileDlg)
	m_fname = _T("");
	m_buf = _T("");
	//}}AFX_DATA_INIT
	m_type = 0;
	m_dirid = 0;
}

CCreatefileDlg::CCreatefileDlg(UINT type,UINT dir_id,CWnd* pParent /*=NULL*/)
	: CDialog(CCreatefileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCreatefileDlg)
	m_fname = _T("");
	m_size = 0;
	m_buf = _T("");
	//}}AFX_DATA_INIT
	m_type = type;
	m_dirid = dir_id;
}


void CCreatefileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCreatefileDlg)
	DDX_Text(pDX, IDC_FNAME, m_fname);
	DDV_MaxChars(pDX, m_fname, 14);
	DDX_Text(pDX, IDC_EDIT1, m_buf);
	DDV_MaxChars(pDX, m_buf, 5120);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCreatefileDlg, CDialog)
	//{{AFX_MSG_MAP(CCreatefileDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCreatefileDlg message handlers

BOOL CCreatefileDlg::OnInitDialog()
{
	if(m_type>0){
		char *buf;
		struct inode *inode;
		m_fname = dir.direct[m_dirid].d_name;

		CEdit *pe;
		pe = (CEdit*)GetDlgItem(IDC_FNAME);
		pe->ModifyStyle(0,ES_READONLY);

		inode = iget(dir.direct[m_dirid].d_ino);
		m_size = inode->di_size;

		buf = new char[m_size];
		int fid;
		fid = _fopen(dir.direct[m_dirid].d_name,FREAD);
		if(fid>=0)
		{
			if(_fread(fid,buf,m_size))
			{
				m_buf = buf;
				_fclose(fid);

				if(m_type==1){
					pe = (CEdit*)GetDlgItem(IDC_NEWFILE_SIZE);
					pe->ModifyStyle(0,ES_READONLY);
					//设置为不可编辑状态
					pe = (CEdit*)GetDlgItem(IDC_EDIT1);
					pe->ModifyStyle(0,ES_READONLY);
				}
			}	
		}
		else{
			iput(inode);
			delete []buf;
			MessageBox("不能打开文件,","系统提示");
			PostMessage(WM_CLOSE);
		}
		UpdateData(false);
	}
	
	return true;
}

void CCreatefileDlg::OnOK() 
{
	// TODO: Add extra validation here
	char *buf;
	UpdateData(true);

	m_size = m_buf.GetLength();
	//新建文件
	if(m_type==0){
		if( (m_fname.GetLength() > 0) &&
			(m_size > 0))
		{	
			int u_ofid = _fcreate(m_fname.GetBuffer(m_fname.GetLength()));
			if(u_ofid>=0)
			{
				buf = new char[m_size];
				strcpy(buf,m_buf.GetBuffer(m_buf.GetLength()));
				_fwrite (u_ofid, buf, m_size);
				_fclose(u_ofid);
				delete []buf;
			}
			else
			{
				MessageBox("创建失败,","系统提示");
				m_fname = "";
				UpdateData(true);
				return;
			}
		}
		else{
			MessageBox("文件名为空或是输入的文件内容大于文件的大小","系统提示");
			return ;
		}
	}
	//
	else if(m_type==2){
		if( (m_fname.GetLength() > 0) && 
			(m_size > 0) )
			{	
			int u_ofid =_fopen(dir.direct[m_dirid].d_name,FWRITE);
			if(u_ofid>=0)
			{
				buf = new char[m_size];
				strcpy(buf,m_buf.GetBuffer(m_buf.GetLength()));
				_fwrite (u_ofid, buf, m_size);
				_fclose(u_ofid);
				delete []buf;
			}
			else
			{
				MessageBox("重写文件失败,","系统提示");
				return;
			}
		}
		else{
			MessageBox("写入错误","系统提示");
			return ;
		}
	}
	//新建文件
	else if(m_type==3){
		if( (m_fname.GetLength() > 0) && 
			(m_size > 0) )
			{	
			int u_ofid =_fopen(dir.direct[m_dirid].d_name,FWRITE);
			if(u_ofid>=0)
			{
				buf = new char[m_size];
				strcpy(buf,m_buf.GetBuffer(m_buf.GetLength()));
				_fwrite (u_ofid, buf, m_size);
				_fclose(u_ofid);
				delete []buf;
			}
			else
			{
				MessageBox("重写文件失败,","系统提示");
				return;
			}
		}
		else{
			MessageBox("写入错误","系统提示");
			return ;
		}
	}

	CDialog::OnOK();
}

⌨️ 快捷键说明

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