?? myfile.cpp
字號(hào):
// MyFile.cpp : implementation file
//
#include "stdafx.h"
#include "Papaz.h"
#include "MyFile.h"
#include "MyFileOut.h"
#include "MyFileDel.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ChunSize 1024
/////////////////////////////////////////////////////////////////////////////
// CMyFile property page
IMPLEMENT_DYNCREATE(CMyFile, CPropertyPage)
CMyFile::CMyFile() : CPropertyPage(CMyFile::IDD)
{
//{{AFX_DATA_INIT(CMyFile)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CMyFile::~CMyFile()
{
}
void CMyFile::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyFile)
DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyFile, CPropertyPage)
//{{AFX_MSG_MAP(CMyFile)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_READ, OnRead)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyFile message handlers
BOOL CMyFile::OnInitDialog()
{
CPropertyPage::OnInitDialog();
::CoInitialize(NULL);
//創(chuàng)建并打開(kāi)數(shù)據(jù)庫(kù)連接對(duì)象
_variant_t vFieldValue;
CString strFieldValue;
m_pCon.CreateInstance(__uuidof(Connection));
m_pCon->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Papaz.mdb","","",NULL);
m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_ListCtrl.InsertColumn(0,_T("編號(hào)"),LVCFMT_IMAGE|LVCFMT_LEFT);
m_ListCtrl.InsertColumn(1,_T("文件名"));
m_ListCtrl.SetColumnWidth(0 ,50);
m_ListCtrl.SetColumnWidth(1 ,180);
//顯示所有記錄
this->Show();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CMyFile::FileToDb(CString StrPath)
{
VARIANT VarChunk;
SAFEARRAY *Psa;
SAFEARRAYBOUND rgsabound[1];
CFile f(StrPath,CFile::modeRead);
BYTE bval[ChunSize+1];
long vIsRead=0;
while(1)
{
vIsRead=f.Read(bval,ChunSize);
if(vIsRead==0)break;
rgsabound[0].cElements=vIsRead;
rgsabound[0].lLbound=0;
Psa=SafeArrayCreate(VT_UI1,1,rgsabound);
for(long index=0;index<vIsRead;index++)
{
FAILED(SafeArrayPutElement(Psa,&index,&bval[index]));
}
VarChunk.vt=VT_ARRAY|VT_UI1;
VarChunk.parray=Psa;
try
{
m_pRs->Fields->GetItem("word")->AppendChunk(VarChunk);
}
catch (_com_error &e)
{
CString str=(char*)e.Description();
}
::VariantClear(&VarChunk);
::SafeArrayDestroyData(Psa);
if(vIsRead<ChunSize)
break;
}
m_pRs->Update();
UpdateData(TRUE);
f.Close();
m_pRs->Close();
this->Show();
}
void CMyFile::DbToFile(CString Str)
{
m_pRs->Update();
UpdateData(TRUE);
m_pRs->MoveLast();
CFile file;
_variant_t FileNumber;
CString strFileNumber;
_variant_t FileValue;
CString strFileValue;
CString t,h;
m_pRs->MoveLast();
while (VARIANT_FALSE==m_pRs->FirstOfFile ) //判斷前面是否還有記錄,有就向下執(zhí)行,否就退出循環(huán)
{
FileNumber=m_pRs->GetCollect("number");
strFileNumber=(char*)_bstr_t(FileNumber);
if(Str==strFileNumber)
{
FileValue=m_pRs->GetCollect("name");
strFileValue=(char*)_bstr_t(FileValue);
//創(chuàng)建文件夾及文件
CFileFind finder;
CString strDir = _T("c:\\Documents and Settings\\All Users\\桌面");
BOOL bDirExist = TRUE;
if(finder.FindFile(strDir))
{
finder.FindNextFile();
if(!finder.IsDirectory())
{
bDirExist = FALSE;
TRACE(_T("存在同名的文件,無(wú)法創(chuàng)建目錄"));
}
}
else
{
bDirExist = ::CreateDirectory(strDir, NULL);
if(!bDirExist)
{
TRACE(_T("創(chuàng)建%s目錄失敗"), strDir);
}
}
if(bDirExist)
{
file.Open(strDir + _T("\\")+strFileValue, CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyNone);
}
//先向分塊內(nèi)存中輸入,然后再?gòu)膬?nèi)存向數(shù)據(jù)庫(kù)中輸入
long lPhotoSize=m_pRs->Fields->Item["word"]->ActualSize; //得到文件的大小
long lIsRead=0;
_variant_t VarChunk;
BYTE buf[ChunSize];
while(lPhotoSize>0)
{
lIsRead=lPhotoSize>=ChunSize?ChunSize:lPhotoSize;
VarChunk=m_pRs->Fields->Item["word"]->GetChunk(lIsRead);
for(long index=0;index<lIsRead;index++)
{
::SafeArrayGetElement(VarChunk.parray,&index,buf+index);
}
file.Write(buf,lIsRead);
lPhotoSize-=lIsRead;
}
break;
}
else
m_pRs->MovePrevious();
}
file.Close();
}
void CMyFile::OnAdd()
{
//創(chuàng)建并打開(kāi)記錄集對(duì)象
m_pRs.CreateInstance(__uuidof(Recordset));
m_pRs->Open("select* from 表1",m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
UpdateData(TRUE);
static char BASED_CODE szfilter[]="all files(*.*)|*.*||";
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szfilter,NULL);//|OFN_OVERWRIRITEPROMPT
if(dlg.DoModal()==IDOK)
{
m_pRs->AddNew();
m_pRs->PutCollect("name",_variant_t(dlg.GetFileName()));
FileToDb(dlg.GetPathName());
MessageBox("輸入完成");
UpdateData(TRUE);
}
}
void CMyFile::OnRead()
{
CMyFileOut f;
CString str;
//創(chuàng)建并打開(kāi)記錄集對(duì)象
m_pRs.CreateInstance(__uuidof(Recordset));
m_pRs->Open("select* from 表1",m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
m_pRs->Update();
UpdateData(TRUE);
f.DoModal();
if(f.m_Number=="")
return;
else
str=f.m_Number;
DbToFile(str);
UpdateData(TRUE);
m_pRs->Close();
MessageBox("輸出完成");
}
void CMyFile::Show()
{
m_ListCtrl.SetFocus();
m_ListCtrl.DeleteAllItems();
m_ListCtrl.SetRedraw(FALSE);
CString sCatID, sCategory;
_variant_t Filednumber;
CString strFieldnumber;
_variant_t Filedname;
CString strFieldname;
//創(chuàng)建并打開(kāi)記錄集對(duì)象
m_pRs.CreateInstance(__uuidof(Recordset));
m_pRs->Open("select* from 表1",m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
m_pRs->MoveLast();
while (VARIANT_FALSE==m_pRs->FirstOfFile )
{
Filednumber=m_pRs->GetCollect("number");
strFieldnumber=(char*)_bstr_t(Filednumber);
Filedname=m_pRs->GetCollect("name");
strFieldname=(char*)_bstr_t(Filedname);
m_ListCtrl.InsertItem(0,strFieldnumber,0);
m_ListCtrl.SetItemText(0,1,strFieldname);
m_pRs->MovePrevious();
}
m_pRs->Close();
m_ListCtrl.SetRedraw(TRUE);
}
void CMyFile::OnDelete()
{
m_pRs.CreateInstance(__uuidof(Recordset));
m_pRs->Open("select* from 表1",m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
CMyFileDel dlg;
if(dlg.DoModal()==IDOK)
{
BOOL b=FALSE;
m_pRs->MoveLast();
CFile f;
_variant_t FileNumber;
CString strFileNumber;
_variant_t FileValue;
CString strFileValue;
CString t,h;
while (VARIANT_FALSE==m_pRs->FirstOfFile )
{
FileNumber=m_pRs->GetCollect("number");
strFileNumber=(char*)_bstr_t(FileNumber);
if(dlg.m_DelName==strFileNumber)
{
m_ListCtrl.DeleteAllItems();
b=TRUE;
m_pRs->Delete(adAffectCurrent);
m_pRs->Update();
m_pRs->Close();
this->Show();
m_pRs.CreateInstance(__uuidof(Recordset));
m_pRs->Open("select* from 表1",m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
m_pRs->MoveLast();
break;
}
else
m_pRs->MovePrevious();
}
if(b==FALSE)
AfxMessageBox("沒(méi)有此記錄");
}
m_pRs->Close();
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -