学习笔记

<script language=javascript type=text/javascript> var gblSpaceTitle = '/u65cb/x27s space';</script>
<script type=text/javascript> function search() { // If the page is dirty and we prompt the user to ok/cancel the navigation, and they // select cancel, window.location.href assignment will throw. In this case, we want to // simply ignore it. try { window.location.href = "http://home.services.spaces.live.com/search/?tp=1&pg=0&sc=0&form=SPDEPS" + "&q=" + encodeURIComponent(document.getElementById("uxp_hdr_searchInput").value); } catch (e) {} } </script>
<script type=text/javascript>function uxp_p(t){t.parentNode.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+t.src+"')";}</script>
<script src="http://shared.live.com/w0cBUAeJzxpl2BKWb!s3qg/header.js" defer type=text/javascript></script>
 
 
 
<script type=text/javascript> function showFrameMenu(p_el) { if ((typeof(Spaces) != "undefined") && Spaces.PartFrame) { Spaces.PartFrame.showMenu(p_el, 'http/x3a/x2f/x2fspaces.live.com/x2fapi.aspx/x3fwx_action/x3daddModule/x26mkt/x3dzh-CN/x26wx_targetSite/x3dPersonalSpaces/x26wxp_url/x3d/x7b0/x7d'); } return false; } var g_siteType = "PersonalSpace"; </script>
Rot
Christine

<script language=javascript> ; </script>
 

1, 指向常量的指针&&指针常量

Char ch[5]=”lisi”;
Const char * pStr=ch;    const char *等同char const *
Char * const *pStr=ch; 指针是常量,指针不可更改,其内容可更改

2, 读写

文件读取操作
FILE *pFile=fopen("1.txt","r");

char ch[100]="0";   //数组被赋值为全零
memset(ch,0,100);//等同于上一句?

//char ch[100];    //如果不把数组赋零,也可以在写入文件中多写一个空字符
fwrite("I Love You",1,strlen("I Love You")+1,pFile);
//memset(ch,0,100);  把数组赋为全零的另一种方法。
fread(ch,1,100,pFile);
fflush(pFile);

3, 获取文件大小

fseek(pFile,0,SEEK_END);  //把文件指针移到文件末尾
int n=ftell(pFile);    //得到文件长度

rewind(pFile);     //把指针移回文件头  fseek(pFile,0,SEEK_BEGIN)
pbuf=new char[n+1];
pbuf[n]=0;      //文件尾加/0作为文件结束符
fread(pbuf,1,n,pFile);

4, 文本和二进制方式。读取和写入的保持一致
文本:写入, 换行(10)è回车--换行(ASCII为13、10)
读取, 回车--换行(ASCII 13、10)è换行(10)

二进制:将数据在内存中的存储形式原样输出到文件中
不管是文本文件还是二进制文件,都可以用文本方式或者二进制方式中的任意一种打开

5, 字符和数字

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

int i=98341;     //非要使他可用,可增加itoa(i,ch,10);
fwrite(&i,4,1,pFile); 

6, C++中文件操作

需要加头文件#include "fstream.h"
ofstream os("3.txt");
os.write("I love you!",strlen("I love you!"));
os.close();  

读文件:

ifstream ifs("3.txt",ios::in);
char ch[100]="0";
memset(ch,0,100)
ifs.read(ch,100);
ifs.close();

//循环读取文件每一行

While(!ifs.getline(ch,100).eof())

{//do something with data in buffer ch

}

下一次重新getline之前,需要 ifs.clear()清除eof标志

7, Win32API函数存取文件

(1)写入
HANDLE hfile;
hfile=CreateFile("6.txt",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dwWrites;
WriteFile(hfile,"I Love You!(5)",strlen("I Love You!(5)"),&dwWrites,NULL);
CloseHandle(hfile);
(2)读取
HANDLE hfile;
hfile=CreateFile("6.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
char ch[100]="0";  //如果只写char ch[dwRead-1];可以在之后用ch[dwRead]=0设结束符
DWORD dwRead;
ReadFile(hfile,ch,100,&dwRead,NULL);
ch[dwRead]=0;
CloseHandle(hfile);

8, SDK方法

(1)写入:

CFile file("7.txt",CFile::modeCreate | CFile::modeWrite);
file.Write("I Love You 1000",strlen("I Love You 1000"));
file.Close();

(2)读取:

CFile file("7.txt",CFile::modeRead);
char *pBuf;
DWORD i=file.GetLength();

pBuf=new char[i+1];
file.Read(pBuf,i);

pBuf[i]=0;

file.Close();

9, 构造文件对话框,存取文件方法

(1)写入:

CFileDialog fileDlg(FALSE);
fileDlg.m_ofn.lpstrTitle="我的文件保存对话框";
fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)/0*.txt/0All Files(*.*)/0*.*/0/0";
//注意lpstrFilter的构造:每个段落后边都要加/0,末尾要加两个/0,括号里的只是显示,实//现在紧跟着的/0后边,此过滤器只为过滤可见文件使用,并不能按所见格式保存。
fileDlg.m_ofn.lpstrDefExt="txt";
if(IDOK==fileDlg.DoModal())
{
     CFile file(fileDlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);
     file.Write("I Love You 1000",strlen("I Love You 1000"));
     file.Close();
}

(2)读取

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 i=file.GetLength();
     pbuf=new char[i+1];   //动态建立缓冲区,值得学习
     pbuf[i]=0;
     file.Read(pbuf,i);
      MessageBox(pbuf);
     file.Close();

}

10, 读写配置文件

CXXXApp::InitInstance(){

// 写在SetRegistryKey(_T("Local AppWizard-Generated Applications"));之后(也可以重新设置表项)

::WriteProfileString("songpeng","sp","song");用来在C:/WINDOWS/win.ini中写入数据。一方面为了兼容十六位程序,另一方面提高程序运行速度
     //在win32中为[HKEY_CURRENT_USER]è[Software]è[Local Appwizard-Generated Applications]è[File]

     CString str;
     ::GetProfileString("songpeng","sp","peng",str.GetBuffer(100),100);

}

11, 读写注册表

读写配置文件的函数WriteProfileString(),GetProfileString()在win32下自动成为注册表的读写。

SetRegistryKey(_T("Local AppWizard-Generated Applications"));用来在注册表的HKEY_CURRENT_USER->Software下增加主键Local AppWizard-Generated Applications
子键及值由WriteProfileString("songpeng","sp","song");增加

(1)写入注册表:

HKEY hkey;
RegCreateKey(HKEY_LOCAL_MACHINE,"Software//MyItem//Name",&hkey);      RegSetValue(hkey,NULL,REG_SZ,"song",strlen("song"));
RegSetValueEx(hKey,”age”,0,REG_DWORD,(CONST BYTE*)&dwAge,4);

     RegCloseKey(hkey);

(2)读取注册表

注意要先获取字段大小

     LONG lValue;
RegQueryValue(HKEY_LOCAL_MACHINE,"Software//MyItem//Name",NULL,&lValue);
     char *buf=new char[lValue];    //注意建立缓冲区方法
RegQueryValue(HKEY_LOCAL_MACHINE,"Software// MyItem//Name",buf,&lValue);

LONG RegQueryValue(
  HKEY hKey,                     // handle to key to query
  LPCTSTR lpSubKey,   // name of subkey to query
  LPTSTR lpValue,               // buffer for returned string
  PLONG lpcbValue             // receives size of returned string
);
If lpValue is NULL, and lpcbValue is non-NULL, the function returns ERROR_SUCCESS, and stores the size of the data, in bytes, in the variable pointed to by lpcbValue. This lets an application determine the best way to allocate a buffer for the value's data. 所以,我们要调用两次RegQueryValue,第一次查询键值长度,第二次获得键值

HKEY hkey;
RegOpenKey(HKEY_LOCAL_MACHINE, "Software//MyItem//Name",&hkey);
//打开主键
DWORD dwType;
DWORD dwAge;
DWORD dwValue;
RegQueryValueEx(hkey,"age",0,&dwType,(LPBYTE)&dwAge,&dwValue);

其他函数:

     RegDeleteKey(); RegDeleteValue();等

第12课 文件操作

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.MFC中 用CFile类,哈哈!简单好用
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

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值