孙鑫VC视频教程笔记之第十二课“文件操作(含注册表操作)”
2008-09-08 08:39
483 查看
文件读写:[/b][/b]
C语言实现:
文件读[/b]:
//方法1:
FILE *pFile=fopen("1.txt","r");
char content[20];
memset(content,0,20); //将数组中的内容全部设置为/0,下一行语句中遇到/0就不读取了
fread(content,1,20,pFile); //读取文件内容,直到遇见/0字符
//方法2:根据文件大小动态声明数组大小
char *content;
fseek(pFile,0,SEEK_END); //将文件指针移动到末尾
long size=ftell(pFile);
//fseek(pFile,0,SEEK_SET); //再此将指针移动到开始位置,从而读取
rewind(pFile); //用途和上一行语句一样
content=new char[size+1]; //多一个用来存放结尾操作符
fread(content,1,size,pFile);
content[size]=0;
MessageBox(content);
fclose(pFile);
文件写:[/b][/b]
[/b]//如果缺少fflush或这fclose函数,程序exe不关闭的话,则输出内容不会从缓冲区中输出到文件中
FILE *pFile=fopen("1.txt","w"); //以读的方式打开
char test[3];
test[0]='a';
test[1]=10; //换行
test[2]='b';
fwrite(test,1,3,pFile); //将输出文件用Utraledit的16机制打开,发现中间多了一个回车0D(13)
//fwrite("hello world",1,strlen("hello world"),pFile);
fseek(pFile,0,SEEK_SET); //将文件指针移动到文件开始位置,用来跳转写入
fflush(pFile); //系统会将缓冲区中的内容立 即写入到文件中,无需fclose操作也能成功保存
fclose(pFile); //关闭文件[/b]
[/b]
[/b]思考题:将数字[/b]12345[/b]写到到文本文件中[/b][/b]
FILE *pFile=fopen("1.txt","w");
char test[5];
//0的ASSII码是48,因为在文本文件中存放的都是字符,所以必须得到指定数字对应的字符
test[0]=1+48;
test[1]=2+48;
test[2]=3+48;
test[3]=4+48;
test[4]=5+48;
fwrite(test,1,5,pFile);
//也可以按以下方式实现:
int test=12345;
char *str;
itoa(test,str,10);
fwrite(str,1,5,pFile);
[/b]
C++语言实现:
文件读:[/b][/b]
[/b]ifstream ifs("1.txt");
char ch[100];
memset(ch,0,100);
ifs.read(ch,100);
ifs.close();
MessageBox(ch);
[/b]
[/b]文件写:[/b][/b]
[/b]ofstream os("1.txt");
os.write("hello world",strlen("hello world"));
os.close();
[/b]
Win 32 API实现:
文件读:[/b][/b]
HANDLE hFile=CreateFile("1.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);
char result[100];
DWORD readed; //实际读取到的字节数
ReadFile(hFile,result,100,&readed,NULL);
result[readed]=0; //结束字符
CloseHandle(hFile);
MessageBox(result);
文件写:[/b][/b]
[/b]HANDLE hFile=CreateFile("1.txt",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,NULL);
DWORD writen;
WriteFile(hFile,"hello world",strlen("hello world"),&writen,NULL);
CloseHandle(hFile);
[/b]
MFC实现:
文件读:[/b][/b]
CFile file("1.txt",CFile::modeRead);
char *buffer;
DWORD length=file.GetLength();
buffer=new char[length+1];
buffer[length]=0;
file.Read(buffer,length);
file.Close();
MessageBox(buffer);
文件写:[/b][/b]
CFile file("1.txt",CFile::modeCreate|CFile::modeWrite);
file.Write("hello world!",strlen("hello world!"));
file.Close();
文件打开保存对话框:[/b][/b]
[/b]文件打开对话框:
[/b]CFileDialog dlg(TRUE);
dlg.m_ofn.lpstrTitle="我的文件打开";
dlg.m_ofn.lpstrFilter="Text Files(.txt)/0*.txt/0All Files(*.*)/0*.*/0/0";
if(IDOK==dlg.DoModal())
{
CFile file(dlg.GetFileName(),CFile::modeRead);
char *buffer;
DWORD length=file.GetLength();
buffer=new char[length+1];
buffer[length]=0;
file.Read(buffer,length);
file.Close();
MessageBox(buffer);
}
文件保存对话框:
CFileDialog dlg(FALSE);
dlg.m_ofn.lpstrTitle="我的文件保存";
dlg.m_ofn.lpstrFilter="Text Files(.txt)/0*.txt/0All Files(*.*)/0*.*/0/0";
dlg.m_ofn.lpstrDefExt="txt";
if(IDOK==dlg.DoModal())
{
CFile file(dlg.GetFileName(),CFile::modeCreate|CFile::modeWrite);
file.Write("hello world!",strlen("hello world!"));
file.Close();
}[/b]
注册表读写:[/b][/b]
[/b]系统默认位置(在[/b]NT[/b]之前是记录在[/b]Win.ini[/b]文件中):[/b][/b]
[/b]读:[/b][/b]
[/b]CString strSection = "My Section";
CString strStringItem = "My String Item";
CWinApp* pApp = AfxGetApp();
CString result=pApp->GetProfileString(strSection, strStringItem, NULL);
MessageBox(result);[/b]
[/b]写:[/b][/b]
[/b]//由MSDN得知,在NT以后,WriteProfileString函数将设置写在注册表中
CString strSection = "My Section";
CString strStringItem = "My String Item";
CWinApp* pApp = AfxGetApp();
pApp->WriteProfileString(strSection, strStringItem, "test");
[/b]
[/b]用户自定义位置:[/b][/b]
[/b]读:[/b][/b]
[/b]LONG strLenth;
//RegQueryValue就是为了得到没有名称或默认的Value的值的
//第一次调用为了得到值的大小
RegQueryValue(HKEY_LOCAL_MACHINE,"software//VS//Test",NULL,&strLenth);
//在MSDN中已经指出RegQueryValue函数的最后一个参数lpcbValue已经包含了终止符号,
//所以不需要在此添加终止符号了,即char *pBuffer=new char[strLenth+1];
char *pBuffer=new char[strLenth];
//第二次调用,才真正是为了得到值
RegQueryValue(HKEY_LOCAL_MACHINE,"software//VS//Test",pBuffer,&strLenth);
MessageBox(pBuffer);
// RegQueryValueEx 函数的使用为了获得指定名称的值
HKEY hKey;
DWORD dwLength; //接收值的长度
DWORD dwType; //接收值的类型
DWORD dwValue; //接收值本身
RegOpenKey(HKEY_LOCAL_MACHINE,"software//VS//Test",&hKey); //打开子项
RegQueryValueEx(hKey,"age",NULL,&dwType,(LPBYTE)&dwValue,&dwLength); //打开值
CString result;
result.Format("%d",dwValue);
MessageBox(result);
[/b]
[/b]写:[/b][/b]
[/b]HKEY hKey;
RegCreateKey(HKEY_LOCAL_MACHINE,"software//VS//Test",&hKey);
//RegSetValue就是为了给没有名称或默认的Value设置值的
RegSetValue(hKey,NULL,REG_SZ,"HELLO WORLD",strlen("HELLO WORLD"));
RegCloseKey(hKey);
//RegSetValueEx 函数为了设置指定名称值存在,当然该函数也可以实现RegSetValue的功能
DWORD dwAge=30;
RegSetValueEx(hKey,"age",0,REG_DWORD,(BYTE*)&dwAge,4);
RegCloseKey(hKey);[/b]
C语言实现:
文件读[/b]:
//方法1:
FILE *pFile=fopen("1.txt","r");
char content[20];
memset(content,0,20); //将数组中的内容全部设置为/0,下一行语句中遇到/0就不读取了
fread(content,1,20,pFile); //读取文件内容,直到遇见/0字符
//方法2:根据文件大小动态声明数组大小
char *content;
fseek(pFile,0,SEEK_END); //将文件指针移动到末尾
long size=ftell(pFile);
//fseek(pFile,0,SEEK_SET); //再此将指针移动到开始位置,从而读取
rewind(pFile); //用途和上一行语句一样
content=new char[size+1]; //多一个用来存放结尾操作符
fread(content,1,size,pFile);
content[size]=0;
MessageBox(content);
fclose(pFile);
文件写:[/b][/b]
[/b]//如果缺少fflush或这fclose函数,程序exe不关闭的话,则输出内容不会从缓冲区中输出到文件中
FILE *pFile=fopen("1.txt","w"); //以读的方式打开
char test[3];
test[0]='a';
test[1]=10; //换行
test[2]='b';
fwrite(test,1,3,pFile); //将输出文件用Utraledit的16机制打开,发现中间多了一个回车0D(13)
//fwrite("hello world",1,strlen("hello world"),pFile);
fseek(pFile,0,SEEK_SET); //将文件指针移动到文件开始位置,用来跳转写入
fflush(pFile); //系统会将缓冲区中的内容立 即写入到文件中,无需fclose操作也能成功保存
fclose(pFile); //关闭文件[/b]
[/b]
[/b]思考题:将数字[/b]12345[/b]写到到文本文件中[/b][/b]
FILE *pFile=fopen("1.txt","w");
char test[5];
//0的ASSII码是48,因为在文本文件中存放的都是字符,所以必须得到指定数字对应的字符
test[0]=1+48;
test[1]=2+48;
test[2]=3+48;
test[3]=4+48;
test[4]=5+48;
fwrite(test,1,5,pFile);
//也可以按以下方式实现:
int test=12345;
char *str;
itoa(test,str,10);
fwrite(str,1,5,pFile);
[/b]
C++语言实现:
文件读:[/b][/b]
[/b]ifstream ifs("1.txt");
char ch[100];
memset(ch,0,100);
ifs.read(ch,100);
ifs.close();
MessageBox(ch);
[/b]
[/b]文件写:[/b][/b]
[/b]ofstream os("1.txt");
os.write("hello world",strlen("hello world"));
os.close();
[/b]
Win 32 API实现:
文件读:[/b][/b]
HANDLE hFile=CreateFile("1.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);
char result[100];
DWORD readed; //实际读取到的字节数
ReadFile(hFile,result,100,&readed,NULL);
result[readed]=0; //结束字符
CloseHandle(hFile);
MessageBox(result);
文件写:[/b][/b]
[/b]HANDLE hFile=CreateFile("1.txt",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,NULL);
DWORD writen;
WriteFile(hFile,"hello world",strlen("hello world"),&writen,NULL);
CloseHandle(hFile);
[/b]
MFC实现:
文件读:[/b][/b]
CFile file("1.txt",CFile::modeRead);
char *buffer;
DWORD length=file.GetLength();
buffer=new char[length+1];
buffer[length]=0;
file.Read(buffer,length);
file.Close();
MessageBox(buffer);
文件写:[/b][/b]
CFile file("1.txt",CFile::modeCreate|CFile::modeWrite);
file.Write("hello world!",strlen("hello world!"));
file.Close();
文件打开保存对话框:[/b][/b]
[/b]文件打开对话框:
[/b]CFileDialog dlg(TRUE);
dlg.m_ofn.lpstrTitle="我的文件打开";
dlg.m_ofn.lpstrFilter="Text Files(.txt)/0*.txt/0All Files(*.*)/0*.*/0/0";
if(IDOK==dlg.DoModal())
{
CFile file(dlg.GetFileName(),CFile::modeRead);
char *buffer;
DWORD length=file.GetLength();
buffer=new char[length+1];
buffer[length]=0;
file.Read(buffer,length);
file.Close();
MessageBox(buffer);
}
文件保存对话框:
CFileDialog dlg(FALSE);
dlg.m_ofn.lpstrTitle="我的文件保存";
dlg.m_ofn.lpstrFilter="Text Files(.txt)/0*.txt/0All Files(*.*)/0*.*/0/0";
dlg.m_ofn.lpstrDefExt="txt";
if(IDOK==dlg.DoModal())
{
CFile file(dlg.GetFileName(),CFile::modeCreate|CFile::modeWrite);
file.Write("hello world!",strlen("hello world!"));
file.Close();
}[/b]
注册表读写:[/b][/b]
[/b]系统默认位置(在[/b]NT[/b]之前是记录在[/b]Win.ini[/b]文件中):[/b][/b]
[/b]读:[/b][/b]
[/b]CString strSection = "My Section";
CString strStringItem = "My String Item";
CWinApp* pApp = AfxGetApp();
CString result=pApp->GetProfileString(strSection, strStringItem, NULL);
MessageBox(result);[/b]
[/b]写:[/b][/b]
[/b]//由MSDN得知,在NT以后,WriteProfileString函数将设置写在注册表中
CString strSection = "My Section";
CString strStringItem = "My String Item";
CWinApp* pApp = AfxGetApp();
pApp->WriteProfileString(strSection, strStringItem, "test");
[/b]
[/b]用户自定义位置:[/b][/b]
[/b]读:[/b][/b]
[/b]LONG strLenth;
//RegQueryValue就是为了得到没有名称或默认的Value的值的
//第一次调用为了得到值的大小
RegQueryValue(HKEY_LOCAL_MACHINE,"software//VS//Test",NULL,&strLenth);
//在MSDN中已经指出RegQueryValue函数的最后一个参数lpcbValue已经包含了终止符号,
//所以不需要在此添加终止符号了,即char *pBuffer=new char[strLenth+1];
char *pBuffer=new char[strLenth];
//第二次调用,才真正是为了得到值
RegQueryValue(HKEY_LOCAL_MACHINE,"software//VS//Test",pBuffer,&strLenth);
MessageBox(pBuffer);
// RegQueryValueEx 函数的使用为了获得指定名称的值
HKEY hKey;
DWORD dwLength; //接收值的长度
DWORD dwType; //接收值的类型
DWORD dwValue; //接收值本身
RegOpenKey(HKEY_LOCAL_MACHINE,"software//VS//Test",&hKey); //打开子项
RegQueryValueEx(hKey,"age",NULL,&dwType,(LPBYTE)&dwValue,&dwLength); //打开值
CString result;
result.Format("%d",dwValue);
MessageBox(result);
[/b]
[/b]写:[/b][/b]
[/b]HKEY hKey;
RegCreateKey(HKEY_LOCAL_MACHINE,"software//VS//Test",&hKey);
//RegSetValue就是为了给没有名称或默认的Value设置值的
RegSetValue(hKey,NULL,REG_SZ,"HELLO WORLD",strlen("HELLO WORLD"));
RegCloseKey(hKey);
//RegSetValueEx 函数为了设置指定名称值存在,当然该函数也可以实现RegSetValue的功能
DWORD dwAge=30;
RegSetValueEx(hKey,"age",0,REG_DWORD,(BYTE*)&dwAge,4);
RegCloseKey(hKey);[/b]
相关文章推荐
- 孙鑫VC视频教程笔记之第五课“在视图上实现文本操作”
- LAMP兄弟连原创视频教程(PHP笔记四--正则表达式,文件,目录操作)
- 孙鑫VC++视频教程(12)文件操作
- 孙鑫VC学习笔记:第十二讲 (一) 介绍常量指针、指针常量,文本、二进制文件
- 孙鑫:第十二讲 文件、注册表、文件对话框的操作
- 注册表操作-->mengwuji驱动教程笔记
- Java基础视频笔记(六):本地文件操作
- 孙鑫VC视频教程笔记之第十课“绘图”
- 寒冰linux视频教程笔记6 文件查找与文件管理
- 孙鑫VC学习笔记:第十二讲 用C语言函数读写文件
- 孙鑫VC视频教程笔记之第十六课(上)“线程同步”
- 孙鑫VC++视频学习笔记之12:文件操作
- 孙鑫VC视频教程笔记之第十七课“进程间的通信
- 孙鑫第12课笔记——文件和注册表的操作
- 孙鑫VC视频教程笔记之第十四课“基于TCP和UDP的Socket编程”
- PHP特级视频教程 笔记心得 第十六集 mysql调优,mysql优化 MySQL基础操作(一)
- 《VC++深入详解》学习笔记---12章文件和注册表操作(2)
- 批处理教程5——如何用批处理文件来操作注册表
- 孙鑫VC视频教程笔记之第十六课(下)“异步套接字编程”
- 孙鑫VC视频教程笔记之第十八课“ActiveX控件”