文件读写和注册表读写

文件读写和注册表读写

添加如下菜单项目:

响应代码:

一、             文件读写

void CFileView::OnFileWrite()

{

    // TODO:在此添加命令处理程序代码

    //读写方式虽然是成对提供给程序员的,但是并不要求对应使用,文件的操作是对文件本身的操作,不关心文件的产生

     //方法一:C语言函数

     //(1)写入一个文本,调用fclose来写入磁盘

/*  FILE *pFile=fopen("1.txt","w");  //文件指针

fwrite("http://volnet.cnblogs.com",1,strlen("http://volnet.cnblogs.com"),pFile);

fclose(pFile);     //关闭文件指针将文件执行更新,使保存在文件缓存中的数据写如到磁盘中去

*/

     //(2)有时候可能要多次写入文件,每次更新如果要关闭再开,比较麻烦,另外对部分数据如日志信息及时更新非常重要,为了保护重要数据不会因为断电等原因而出现读写错误,引入fflush()

/*  FILE *pFile=fopen("2.txt","w");  //文件指针

fwrite("http://mymsdn.photo.163.com",1,strlen("http://mymsdn.photo.163.com"),pFile);

fflush(pFile);     //实时更新数据文件指针将随应用程序一起关闭

*/

     //(3)在文件头加字符

/*  FILE *pFile=fopen("3.txt","w");  //文件指针

fwrite("http://volnet.cnblogs.com",1,strlen("http://volnet.cnblogs.com"),pFile);

fseek(pFile,0,SEEK_SET);    //定位指针为SEEK_SET(指向文件Begin位置)偏移量为‘0’

fwrite("ftp:",1,strlen("ftp:"),pFile);    //在当前指针位置写入如下字符

fclose(pFile);     //关闭文件指针将文件执行更新,使保存在文件缓存中的数据写如到磁盘中去

*/

     //(4)在文本方式存储的二进制文件中10(换行)将自动被13 10()回车换行所替换

/*  FILE *pFile=fopen("4.txt","wb"); //wb(在w后面加b)加了b表示是二进制方式读/写操作(请确保读写方式是统一的)

//FILE *pFile=fopen("4.txt","w");    //供给测试的"w"方式

char ch[3];

ch[0]='a';

ch[1]=10;

ch[2]='b';

fwrite(ch,1,3,pFile);

fclose(pFile);

*/

     //(5)存取数字的时候记得是存取ASCII而不是单纯的数字

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

int i=98341;

char ch[6];   //注意这里是6,可以断点调试,如果是5的话将出现乱码,文件运行的时候会出错

/*ch[0]=9+48;

ch[1]=8+48;

ch[2]=3+48;

ch[3]=4+48;

ch[4]=1+48;   */  //相当于itoa(i,ch,10);

    /*itoa(i,ch,10);  //以十进制方式转换数字到一个字符数组

fwrite(ch,1,5,pFile);

fclose(pFile);

*/

     //方法二:C++ /*但是本方法在2003中出现错误,不过该类方法将在C++演化中逐渐淘汰*/

/*  ofstream ofs("6.txt"); //调用该结构体需要包含头文件ofstream.h

ofs.write("http://volnet.cnblogs.com",strlen("http://volnet.cnblogs.com"));

ofs.close();

*/

    //方法三:WINDOWS API

/*  HANDLE hFile;

hFile=CreateFile("7.txt",GENERIC_WRITE,0,NULL,CREATE_NEW,

FILE_ATTRIBUTE_NORMAL,NULL);

DWORD dwWrites;

WriteFile(hFile,"http://volnet.cnblogs.com",strlen("http://volnet.cnblogs.com"),

&dwWrites,NULL);

CloseHandle(hFile);

*/

    //方法四:MFC

/*  CFile file("8.txt",CFile::modeCreate | CFile::modeWrite);

file.Write("http://volnet.cnblogs.com",strlen("http://volnet.cnblogs.com"));

file.Close();

*/

    //以下是文件对话框

/*  CFileDialog fileDlg(FALSE); //当FALSE的时候是“另存为”对话框,当TRUE的时候是“打开文件”对话框

//Set to TRUE to construct a File Open dialog box or FALSE to construct a File Save As dialog box.

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

fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0"; //设置过滤器,可以过滤文件后缀最后是2个\0

fileDlg.m_ofn.lpstrDefExt="txt"; //默认保存文件名

if(IDOK==fileDlg.DoModal())

{

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

file.Write("http://volnet.cnblogs.com",strlen("http://volnet.cnblogs.com"));

file.Close();

}

*/

}

void CFileView::OnFileRead()

{

    // TODO:在此添加命令处理程序代码

    //读写方式虽然是成对提供给程序员的,但是并不要求对应使用,文件的操作是对文件本身的操作,不关心文件的产生

     //方法一:C语言函数

     //(1

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

char ch[100];

memset(ch,0,100);  //设置100个字符全为0

fread(ch,1,100,pFile);

MessageBox(ch);    //当遇到0的时候就截止了

fclose(pFile);

*/

     //(2

/*  FILE *pFile=fopen("2.txt","r");

char *pBuf;

fseek(pFile,0,SEEK_END);    //将当前指针定位到最后

int len=ftell(pFile);  //ftell返回当前指针的值,与上一句一起统计了文本数量

pBuf=new char[len+1];  //为BUFFER分配了内存空间

rewind(pFile);         //The Rewind method rewinds this iterator to the beginning of its associated path.指针转到开头

fread(pBuf,1,len,pFile);

pBuf[len]=0;  //最后一个写入0供MessageBox做中断

MessageBox(pBuf);

fclose(pFile);

*/

     //(3)对应写方法中的(4),用来区别二进制方式与文本方式

/*  FILE *pFile=fopen("4.txt","rb");

//FILE *pFile=fopen("4.txt","r");    //供给测试的"r"方式

char ch[100];

fread(ch,1,3,pFile);

ch[3]=0;

MessageBox(ch);

fclose(pFile);

*/

     //方法二:C++ /*但是本方法在2003中出现错误,不过该类方法将在C++演化中逐渐淘汰*/

/*  ifstream ifs("6.txt");

char ch[100];

memset(ch,0,100);

ifs.read(ch,100);

ifs.close();

MessageBox(ch);

*/

    //方法三:WINDOWS API

/*  HANDLE hFile;

hFile=CreateFile("7.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,

FILE_ATTRIBUTE_NORMAL,NULL);

char ch[100];

DWORD dwReads;

ReadFile(hFile,ch,100,&dwReads,NULL);

ch[dwReads]=0;

CloseHandle(hFile);

MessageBox(ch);

*/

    //方法四:MFC

/*  CFile file("8.txt",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);

*/

    //以下是文件对话框

/*  CFileDialog fileDlg(TRUE);  //当FALSE的时候是“另存为”对话框,当TRUE的时候是“打开文件”对话框

//Set to TRUE to construct a File Open dialog box or FALSE to construct a File Save As dialog box.

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

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

fileDlg.m_ofn.lpstrFilter="文本文件(*.txt,*.h)\0*.txt;*.h\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 CFileView::OnRegWrite()

{

    // TODO:在此添加命令处理程序代码

    //写注册表

     HKEY hKey;

     DWORD dwAge=30;

     RegCreateKey(HKEY_LOCAL_MACHINE,"Software\\http://volnet.cnblogs.com\\admin",&hKey);    //\\admin表示建立一个目录admin

     RegSetValue(hKey,NULL,REG_SZ,"zhangsan",strlen("zhangsan"));//设置键值

     //RegSetValueEx(hKey,"age",0,REG_DWORD,(CONST BYTE*)&dwAge,4);   //RegSetValue只能写REG_SZ类数据也就是String,写String类型外的其他类型

     RegCloseKey(hKey);

}

void CFileView::OnRegRead()

{

    // TODO:在此添加命令处理程序代码

    //读注册表

     //读String类型

/*  LONG lValue;

RegQueryValue(HKEY_LOCAL_MACHINE,"Software\\http://volnet.cnblogs.com\\admin",

NULL,&lValue);

char *pBuf=new char[lValue];

RegQueryValue(HKEY_LOCAL_MACHINE,"Software\\http://volnet.cnblogs.com\\admin",

pBuf,&lValue);

MessageBox(pBuf);

*/

     //读String类型外的其他类型

     HKEY hKey;

     RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\http://volnet.cnblogs.com\\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);

}

三、             初始化(注册表读写)

在APP类中,InitInstance添加代码

代码位置位于SetRegistryKey(_T("应用程序向导生成的本地应用程序"));之后("应用程序向导生成的本地应用程序"这个为应用程序在注册表中的标题,类似于树根)

代码与之前按钮一样

在16位系统中不使用注册表,而使用win.ini,(该文件存在系统目录(XP)C:\WINDOWS下),为了兼容16位系统,经常需要写win.ini来预存信息。

     //WINDOWS API方法,因此要加“::

/*  ::WriteProfileString("http://volnet.cnblogs.com","admin","volnet");   //写win.ini

//读win.ini

CString str; 

::GetProfileString("http://volnet.cnblogs.com","admin","lisi",

str.GetBuffer(100),100);

AfxMessageBox(str);    //因为App类不是从CWnd类派生而来,因此不能使用MessageBox

*/

     //CWinApp::WriteProfileString 因此无须“::

/*  WriteProfileString("http://volnet.cnblogs.com","admin","volnet");

CString str;

str=GetProfileString("http://volnet.cnblogs.com","admin");

AfxMessageBox(str);

*/

 

 

 //  测试
 HKEY hKey = NULL;
 DWORD dwValue = 0x50d646e0;
 DWORD dwValueLen = sizeof(dwValue);
 DWORD dwErrorCode = ERROR_SUCCESS;

 dwErrorCode = RegCreateKey(
  HKEY_CURRENT_USER,
  _T("Software\\Test"),
  &hKey
  );
 if (ERROR_SUCCESS != dwErrorCode) {
  return FALSE;
 }
 
 dwErrorCode = RegSetValueEx(
  hKey,
  _T("midas"),
  0,
  REG_BINARY,
  (CONST BYTE*)&dwValue,
  dwValueLen
  ); 
 if (ERROR_SUCCESS != dwErrorCode) {
  OutputDebugString(_T("Error!\n"));
 }
 RegCloseKey(hKey);

4年北京 10:10:30
http://blog.csdn.net/lpc_china/article/details/5875745
4年北京 10:11:33
http://blog.163.com/shengyong24@126/blog/static/170607811201010109636963/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值