您的位置:首页 > 其它

VC:读写文件 及 注册表

2012-02-01 11:58 260 查看
void CFileView::OnFileWrite()

{

// TODO: Add your command handler code here

CFileDialog fileDlg(FALSE);

fileDlg.m_ofn.lpstrTitle="我的文件保存对话框";

fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";

fileDlg.m_ofn.lpstrDefExt="txt";

if(IDOK==fileDlg.DoModal())

{

CFile file(fileDlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);

file.Write("http://www.gaoyuan.org",strlen("http://www.gaoyuan.org"));

file.Close();

}

}

void CFileView::OnFileRead()

{

// TODO: Add your command handler code here

CFileDialog fileDlg(TRUE);

fileDlg.m_ofn.lpstrTitle="我的文件打开对话框";

fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";

if(IDOK==fileDlg.DoModal())

{

CFile file(fileDlg.GetFileName(),CFile::modeRead);

char *pBuf;

DWORD dwFileLen;

dwFileLen=file.GetLength();

pBuf=new char[dwFileLen+1];

pBuf[dwFileLen]=0;

file.Read(pBuf,dwFileLen);

file.Close();

MessageBox(pBuf);

}

}

=================================================

串行化读写

void CGraphicView::OnFileWrite()

{

// TODO: Add your command handler code here

CFile file("1.txt",CFile::modeCreate | CFile::modeWrite);

CArchive ar(&file,CArchive::store);

int i=4;

char ch='a';

float f=1.3f;

CString str("http://www.sunxin.org");

ar<<i<<ch<<f<<str;

}

void CGraphicView::OnFileRead()

{

// TODO: Add your command handler code here

CFile file("1.txt",CFile::modeRead);

CArchive ar(&file,CArchive::load);

int i;

char ch;

float f;

CString str;

CString strResult;

ar>>i>>ch>>f>>str;

strResult.Format("%d,%c,%f,%s",i,ch,f,str);

MessageBox(strResult);

}

==============================================================

void CFileView::OnRegWrite()

{

// TODO: Add your command handler code here

HKEY hKey;

DWORD dwAge=30;

RegCreateKey(HKEY_LOCAL_MACHINE,"Software\\http://www.gaoyuan.org\\admin",&hKey);

RegSetValue(hKey,NULL,REG_SZ,"zhangsan",strlen("zhangsan"));

RegSetValueEx(hKey,"age",0,REG_DWORD,(CONST BYTE*)&dwAge,4);

RegCloseKey(hKey);

}

void CFileView::OnRegRead()

{

// TODO: Add your command handler code here

HKEY hKey;

RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\http://www.gaoyuan.org\\admin",&hKey);

DWORD dwType;

DWORD dwValue;

DWORD dwAge;

RegQueryValueEx(hKey,"age",0,&dwType,(LPBYTE)&dwAge,&dwValue);

CString str;

str.Format("age=%d",dwAge);

MessageBox(str);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: