`
java-mans
  • 浏览: 11413653 次
文章分类
社区版块
存档分类
最新评论

MFC(文件操作,孙鑫C++第十二讲笔记整理)

 
阅读更多

1.常量指针与指针常量的区分

char ch[5]="lisi";

const char *pStr=ch;//const*之前,表明指针指向的内容为常量,即为常量指针

char * const pStr=ch;//const*之后,表明指针的地址不能改变,即为指针常量

明白?

2.对文件读写的三种方法

1.C

FILE *pFile=fopen("1.txt","w");

fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile);

//fseek(pFile,0,SEEK_SET);

//fwrite("ftp:",1,strlen("ftp:"),pFile);

//fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile);

fclose(pFile);*/

//fflush(pFile);

2.C++

/* ofstream ofs("4.txt");

ofs.write("http://www.sunxin.org",strlen("http://www.sunxin.org"));

ofs.close();*/

要包括头文件 "fstream.h"

3.MFCCFile类,哈哈!简单好用

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.sunxin.org",strlen("http://www.sunxin.org"));

file.Close();

}

4.利用win32 API函数 CreateFile(),WriteFile()

4.注册表读写

1.win.ini的读写

//::WriteProfileString("http://www.sunxin.org","admin","zhangsan");

/* CString str;


::GetProfileString("http://www.sunxin.org","admin","lisi",

str.GetBuffer(100),100);

AfxMessageBox(str);*/

2.注册表的读写

HKEY hKey;

DWORD dwAge=30;

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

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

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

RegCloseKey(hKey);以上是写入

C语言的文件操作:在菜单中定义两个文件操作的菜单选项

void CFileTestView::OnXieru() 
{
	// TODO: Add your command handler code here
	FILE *file;
	file=fopen("demo1.txt","w");
	fwrite("hello,this is demo",1,strlen("hello,this is demo"),file);
	fclose(file);//因为C语言的文件操作是通过缓冲区来完成的,如果没有这句的话,
	//运行程序之后,如果没有关闭程序,则demo1.txt的内容读取不了
	
	//也可以使用如下代码解决问题,以时刻更新文件
	//fflush(file);
	
	
}

void CFileTestView::OnDuqu() 
{
	// TODO: Add your command handler code here
	FILE *file;
	file=fopen("demo1.txt","r");
	char ch[100];//定义一个字符数组来保存读取的值
	fread(ch,1,100,file);//使用读取文件的函数,
	MessageBox(ch);//输出读取的文件,因为文件并没有100个字符,所以后面会有乱码

	
}

解决上面出现乱码的几个方法

1在写入文件的时候,strlen("XXX")+1,则最后写入的字符是'\0'刚好是用于判断字符串结束的标志

2不用改变写操作,直接:

	FILE *file;
	file=fopen("demo1.txt","r");
	char ch[100];
	memset(ch,0,100);//把数组都初始化为0,当读取到0的时候,字符串结束符,你懂的
	fread(ch,1,100,file);
	MessageBox(ch);


3使用C函数fseek和ftell的配合

FILE *file;
	char *ch;//这里暂时不知道大小,所以后面动态分配内存,char ch[len]+1;这是错误的,数组定义的时候不能有变量
	file=fopen("demo1.txt","r");
	fseek(file,0,SEEK_END);
	int len=ftell(file);
	ch=new char[len+1];//分配多一个字节,用以存文件结束符
	fseek(file,0,SEEK_SET);//读取文件之前,这里记得把指针位置调回到开始位置,也可以使用rewind(file)函数
	fread(ch,1,len,file);
	ch[len]=0;
	MessageBox(ch);


写入文件的时候:

	FILE *file;
	file=fopen("demo2.txt","w");
	char ch[3];
	ch[0]='a';
	ch[1]=10;//表示换行的ASCII码
	ch[2]='b';
	fwrite(ch,1,3,file);
	fclose(file);

这个时候看到的文件属性显示的是4个字节而不是3个字节,这是因为默认的fopen类型是文本类型,换行前面自动加了一个字节的回车

读取文件的时候:

         FILE *file;
	file=fopen("demo2.txt","r");
	char ch[100];
	fread(ch,1,3,file);
	ch[3]=0;
	MessageBox(ch);
	fclose(file);

写入文件用了4个字节,读取文件用3个字节,却能正常显示,这是因为文本读取方式,遇到换行和回车的ASCII码的时候,自动组合成了换行

假设,写入方式跟上面一样,用文本方式写入

而读取方式使用二进制读取

	FILE *file;
	file=fopen("demo2.txt","rb");
	char ch[100];
	fread(ch,1,3,file);
	ch[3]=0;
	MessageBox(ch);
	fclose(file);


则读取是一个a,为什么呢,因为写进去了4个字符,而读取的是3个字符。

写入和读取都是二进制形式的话,都能正常读取,写入的是3个字节



所以写入和读取的方式要一致,同为文本或同为二进制

二进制文件不要用文本方式去读取,统一按照二进制去读取,是不会出错的。

不管是二进制文件还是文本文件,都可以使用二进制方式去打开,进行写入和读取。

对二进制,如果以文本方式去打开和写入,就可能会出现问题。

往文件输入数字:

FILE *file;
	file=fopen("demo4.txt","w");
	char *p="98341";
	fwrite(p,1,5,file);
	fclose(file);

直接int =98341;fwrite(&i,4,1,file);这样只会输出乱码。

下面的方法是等价的

FILE *file;
	file=fopen("demo5.txt","w");
	char ch[5];
	ch[0]='9';
	ch[1]='8';
	ch[2]='3';
	ch[3]=48+4;
	ch[4]='0'+1;
	fwrite(ch,1,5,file);
	fclose(file);

下面是C++方法文件操作:

ofstream xieru("demo6.txt");
	xieru.write("hello,use c++ iostream",strlen("hello,use c++ iostream"));
	xieru.close();


ifstream xianshi("demo6.txt");
	char ch[100];
	memset(ch,0,100);
	xianshi.read(ch,100);
	MessageBox(ch);
	xianshi.close();


也可以这样:

	ofstream xieru("demo7.txt");
	xieru<<"hello,use c++ iostream"<<endl;
	xieru.close();


	ifstream xianshi("demo7.txt");
	char c;
	CString cstring="";
	while(xianshi>>c)
		cstring+=c;
	MessageBox(cstring);


异曲同工之妙,下面是分别是WIN API 和MFC的文件操作展示

API:

HANDLE handle=CreateFile("demo9.txt",GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
	
	DWORD count;
	WriteFile(handle,"use c++ to show the io oper",strlen("use c++ to show the io oper"),&count,NULL);
	
	CloseHandle(handle);


HANDLE handle;
	handle=CreateFile("demo9.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

	char ch[100];
	memset(ch,0,100);
	DWORD geshu;
	ReadFile(handle,&ch,100,&geshu,NULL);
	MessageBox(ch);
	CloseHandle(handle);


或者:

HANDLE handle;
	handle=CreateFile("demo9.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	
	char ch[100];
	DWORD geshu;
	ReadFile(handle,ch,100,&geshu,NULL);
	ch[geshu]=0;
	MessageBox(ch);
	CloseHandle(handle);


MFC:

CFile cf;
	cf.Open("demo10.txt",CFile::modeWrite|CFile::modeCreate);
	cf.Write("hello,use MFC to show",strlen("hello,use MFC to show"));
	cf.Close();


或:

	CFile cf("demo10.txt",CFile::modeWrite|CFile::modeCreate);
	cf.Write("hello,use MFC to show",strlen("hello,use MFC to show"));
	cf.Close();


读取:

CFile cf("demo10.txt",CFile::modeRead);
	
	int len=cf.GetLength();
	char *ch=new char[len+1];
	cf.Read(ch,len);
	ch[len]=0;
	MessageBox(ch);cf.close();

MFC的文件操作简单很多,真的。

CFileDialog cfileDlg(false);//文件对话框
	cfileDlg.m_ofn.lpstrDefExt="txt";
	cfileDlg.m_ofn.lpstrTitle="我的文件保存对话框";
	cfileDlg.m_ofn.lpstrFilter="Text File(*.txt)\0*.txt\0All File(*.*)\0*.*\0\0";
	//设置过滤器,是一对一对的值,每个字符串后面有个\0,结束的时候需要两个\0

	if(IDOK==cfileDlg.DoModal())
	{
		CFile cfile(cfileDlg.GetPathName(),CFile::modeCreate|CFile::modeWrite);
		cfile.Write("hello,use CFileDialog!!!",strlen("hello,use CFileDialog!!!"));
		cfile.Close();

	}


	CFileDialog cfileDlg(TRUE);
	cfileDlg.m_ofn.lpstrTitle="我的打开对话框";
	cfileDlg.m_ofn.lpstrFilter="Test File(*.txt)\0*.txt\0All File(*.*)\0*.*\0\0";//设置过滤器
	if(IDOK==cfileDlg.DoModal())
	{
		CFile cfile(cfileDlg.GetPathName(),CFile::modeRead);
		
			char *ch;
			int len=cfile.GetLength();
			ch=new char[len+1];
			cfile.Read(ch,len);
			ch[len]=0;
			cfile.Close();
			MessageBox(ch);

		
	}


跟CColorDialog和CFontDialog一样,都是模态对话框

下面讲的是 如何修改与获取win.ini文件的内容,如何修改获取删除注册表的内容:

在CXXAPP文件的InitInstance函数中添加

	::WriteProfileString("afei@qq.com","admin","maikefeng");

将段名为afei@qq.com,键值为amdin,value为maikefeng

char ch[100];
	::GetProfileString("afei@qq.com","admin","morenzhi",ch,100);
	AfxMessageBox(ch);


或者:

CString cstring;
	::GetProfileString("afei@qq.com","admin","morenzhi",cstring.GetBuffer(100),100);
	AfxMessageBox(cstring);

CXXAPP类中 继承了WriteProfileString和GetProfileString方法,跟WIN SDK的不一样,修改的是注册表

SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	WriteProfileString("afei@qq.com","admin","maikefeng");

其中,SetRegistryKey中的字符串的值是可以改变的

在Hkey Current user--->software---->




void CFileTestView::OnXieruzhucebiao() 
{
	// TODO: Add your command handler code here
	
	HKEY hkey;
	::RegCreateKey(HKEY_LOCAL_MACHINE,"software\\adminqq\\afei",&hkey);
	::RegSetValue(hkey,NULL,REG_SZ,"6345",strlen("6345"));
	::RegCloseKey(hkey);

}

void CFileTestView::OnDuquzhucebiao() 
{
	// TODO: Add your command handler code here
	
	LONG ivalue;
	::RegQueryValue(HKEY_LOCAL_MACHINE,"software\\adminqq\\afei",NULL,&ivalue);
	char *ch=new char[ivalue];
	::RegQueryValue(HKEY_LOCAL_MACHINE,"software\\adminqq\\afei",ch,&ivalue);
	MessageBox(ch);
}

可以调整类型的,自己查MSDN

void CFileView::OnRegWrite() 
{
	// TODO: Add your command handler code here
	HKEY hKey;
	DWORD dwAge=30;
	RegCreateKey(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.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
/*	LONG lValue;
	RegQueryValue(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.org\\admin",
		NULL,&lValue);
	char *pBuf=new char[lValue];
	RegQueryValue(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.org\\admin",
		pBuf,&lValue);
	MessageBox(pBuf);*/
	HKEY hKey;
	RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.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);
}






分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics