Windows编程中各种操作文件的方法
2013-03-02 11:01
351 查看
windows编程中文件操作有以下几种常见方法:
1.C语言中文件操作。
2.C++语言中的文件操作。
3.Win32 API函数文件操作。
4.MFC CFile类文件操作。
5.MFC CFileDialog类的文件操作。
6.注册表文件操作。
下面我来详细说明一下各种文件操作方法:
1. C语言中文件操作.需要包含的头文件STDIO.H
写入文件:
FILE *pfile=fopen("C.txt","w");//以写的方式打开C.txt文件。
fwrite("Welcome to VCFans!",1,strlen("Welcome to VCFans!"),pfile);//将数据写入文件。
fflush(pfile);//刷新缓冲区。将缓冲区数据写入文件
fclose(pfile);//关闭文件
读取文件:
FILE *pfile=fopen("C.txt","r");//以读的方式打开C.txt文件。
char FileContent[100];
memset(FileContent,0,100);//初始化FileContent
fread(FileContent,1,100,pfile);//将刚才C.txt文件中的内容读入到FileContent
MessageBox(FileContent);//输出结果
fclose(pfile);//关闭文件
#include <stdio.h>
#include <afx.h>
void main()
{
LPCTSTR lp = "hello";
short s = 70;
long l = 200;
double d = 20.5;
char buf[200];
sprintf(buf, "%s %d %d %f\n", lp, s, l, d);
FILE *fp;
fp = fopen("c:\\ais1.txt", "wt");
fprintf(fp, "%s", buf);
fclose(fp);
}
2.C++语言中的文件操作。需要包含的头文件fstream.h
C++代码
写入文件:
ofstream ofs("C++.txt");//建立ofstream对像。
ofs.write("Welcome to VCFans!",strlen("Welcome to VCFans!"));//将数据写入文件
ofs.close();//关闭ofstream对象。
读取文件:
ifstream ifs("C++.txt");
char FileContent[100];
memset(FileContent,0,100);//初始化FileContent
ifs.read(FileContent,100);//读取数据
ifs.close();//关闭ifstream对像
MessageBox(FileContent);//输出结果
3.Win32 API函数文件操作。需要包含的头文件winbase.h,需要类库:kernel32.lib
C++代码
写入文件:
HANDLE hFile;//定义一个句柄。
hFile=CreateFile("API.txt",
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);//使用CreatFile这个API函数打开文件
DWORD Written;
WriteFile(hFile,"Welcome to VCFans!",strlen("Welcome to VCFans!"),&Written,NULL);//写入文件
CloseHandle(hFile);//关闭句柄
读取文件:
HANDLE hFile;//定义一个句柄。
hFile=CreateFile("API.txt",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);//使用CreatFile这个API函数打开文件
DWORD dwDataLen;
char FileContent[100];
ReadFile(hFile,FileContent,100,&dwDataLen,NULL);//读取数据
FileContent[dwDataLen]=0;//将数组未尾设零。
CloseHandle(hFile);//关闭句柄
MessageBox(FileContent);//输出结果
4.MFC CFile类文件操作。需要包含的头文件afx.h
C++代码
写入文件:
CFile file("CFile.txt",CFile::modeCreate| CFile::modeWrite);//构造CFile对象
file.Write("Welcome to VCFans !",strlen("Welcome to VCFans !"));//写入数据到文件
file.Close();//关闭CFile对象。
读取文件:
CFile file("CFile.txt",CFile::modeRead);//构造CFile对象
char FileContent[100];
memset(FileContent,0,100);//初始化FileContent
file.Read(FileContent,100);//读入数据
file.Close();//关闭文件对象
MessageBox(FileContent);//输出数据
5.MFC CFileDialog类的文件操作。需要包含的头文件Afxdlgs.h
C++代码
写入文件:
CFileDialog fileDlg(FALSE,"txt","CFileDialog.txt");//建立CFileDialog对象
if(IDOK==fileDlg.DoModal())
{
CFile file(fileDlg.GetFileName(),CFile::modeCreate| CFile::modeWrite);//构造CFile对象
file.Write("Welcome to VCFans !",strlen("Welcome to VCFans !"));//写入数据到文件
file.Close();
};
读取文件:
CFileDialog fileDlg(TRUE,"txt","CFileDialog.txt");//建立CFileDialog对象
if(IDOK==fileDlg.DoModal())
{
CFile file(fileDlg.GetFileName(),CFile::modeRead);//构造CFile对象
char FileContent[100];
memset(FileContent,0,100);//初始化FileContent
file.Read(FileContent,100);//读入数据
file.Close();//关闭文件对象
MessageBox(FileContent);
};
6.注册表文件操作。
C++代码
写入注册表:
HKEY hKey;
DWORD dwSex=1;
RegCreateKey(HKEY_LOCAL_MACHINE,"Software\\vcfans\\reg",&hKey);//打开注册表键
RegSetValueEx(hKey,"sex",0,REG_DWORD,(CONST
BYTE*)&dwSex,4);//写入注册表数据
RegCloseKey(hKey);//关闭注册表键
读注册表:
HKEY hKey;
RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\vcfans\\reg",&hKey);//打开注册表键
DWORD dwType;
DWORD dwValue;
DWORD dwSex;
RegQueryValueEx(hKey,"sex",0,&dwType,(LPBYTE)&dwSex,&dwValue);//查询注册表数据
RegCloseKey(hKey);//关闭注册表键
CString str;
str.Format("sex=%d",dwSex);
MessageBox(str);
1.C语言中文件操作。
2.C++语言中的文件操作。
3.Win32 API函数文件操作。
4.MFC CFile类文件操作。
5.MFC CFileDialog类的文件操作。
6.注册表文件操作。
下面我来详细说明一下各种文件操作方法:
1. C语言中文件操作.需要包含的头文件STDIO.H
写入文件:
FILE *pfile=fopen("C.txt","w");//以写的方式打开C.txt文件。
fwrite("Welcome to VCFans!",1,strlen("Welcome to VCFans!"),pfile);//将数据写入文件。
fflush(pfile);//刷新缓冲区。将缓冲区数据写入文件
fclose(pfile);//关闭文件
读取文件:
FILE *pfile=fopen("C.txt","r");//以读的方式打开C.txt文件。
char FileContent[100];
memset(FileContent,0,100);//初始化FileContent
fread(FileContent,1,100,pfile);//将刚才C.txt文件中的内容读入到FileContent
MessageBox(FileContent);//输出结果
fclose(pfile);//关闭文件
#include <stdio.h>
#include <afx.h>
void main()
{
LPCTSTR lp = "hello";
short s = 70;
long l = 200;
double d = 20.5;
char buf[200];
sprintf(buf, "%s %d %d %f\n", lp, s, l, d);
FILE *fp;
fp = fopen("c:\\ais1.txt", "wt");
fprintf(fp, "%s", buf);
fclose(fp);
}
2.C++语言中的文件操作。需要包含的头文件fstream.h
C++代码
写入文件:
ofstream ofs("C++.txt");//建立ofstream对像。
ofs.write("Welcome to VCFans!",strlen("Welcome to VCFans!"));//将数据写入文件
ofs.close();//关闭ofstream对象。
读取文件:
ifstream ifs("C++.txt");
char FileContent[100];
memset(FileContent,0,100);//初始化FileContent
ifs.read(FileContent,100);//读取数据
ifs.close();//关闭ifstream对像
MessageBox(FileContent);//输出结果
3.Win32 API函数文件操作。需要包含的头文件winbase.h,需要类库:kernel32.lib
C++代码
写入文件:
HANDLE hFile;//定义一个句柄。
hFile=CreateFile("API.txt",
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);//使用CreatFile这个API函数打开文件
DWORD Written;
WriteFile(hFile,"Welcome to VCFans!",strlen("Welcome to VCFans!"),&Written,NULL);//写入文件
CloseHandle(hFile);//关闭句柄
读取文件:
HANDLE hFile;//定义一个句柄。
hFile=CreateFile("API.txt",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);//使用CreatFile这个API函数打开文件
DWORD dwDataLen;
char FileContent[100];
ReadFile(hFile,FileContent,100,&dwDataLen,NULL);//读取数据
FileContent[dwDataLen]=0;//将数组未尾设零。
CloseHandle(hFile);//关闭句柄
MessageBox(FileContent);//输出结果
4.MFC CFile类文件操作。需要包含的头文件afx.h
C++代码
写入文件:
CFile file("CFile.txt",CFile::modeCreate| CFile::modeWrite);//构造CFile对象
file.Write("Welcome to VCFans !",strlen("Welcome to VCFans !"));//写入数据到文件
file.Close();//关闭CFile对象。
读取文件:
CFile file("CFile.txt",CFile::modeRead);//构造CFile对象
char FileContent[100];
memset(FileContent,0,100);//初始化FileContent
file.Read(FileContent,100);//读入数据
file.Close();//关闭文件对象
MessageBox(FileContent);//输出数据
5.MFC CFileDialog类的文件操作。需要包含的头文件Afxdlgs.h
C++代码
写入文件:
CFileDialog fileDlg(FALSE,"txt","CFileDialog.txt");//建立CFileDialog对象
if(IDOK==fileDlg.DoModal())
{
CFile file(fileDlg.GetFileName(),CFile::modeCreate| CFile::modeWrite);//构造CFile对象
file.Write("Welcome to VCFans !",strlen("Welcome to VCFans !"));//写入数据到文件
file.Close();
};
读取文件:
CFileDialog fileDlg(TRUE,"txt","CFileDialog.txt");//建立CFileDialog对象
if(IDOK==fileDlg.DoModal())
{
CFile file(fileDlg.GetFileName(),CFile::modeRead);//构造CFile对象
char FileContent[100];
memset(FileContent,0,100);//初始化FileContent
file.Read(FileContent,100);//读入数据
file.Close();//关闭文件对象
MessageBox(FileContent);
};
6.注册表文件操作。
C++代码
写入注册表:
HKEY hKey;
DWORD dwSex=1;
RegCreateKey(HKEY_LOCAL_MACHINE,"Software\\vcfans\\reg",&hKey);//打开注册表键
RegSetValueEx(hKey,"sex",0,REG_DWORD,(CONST
BYTE*)&dwSex,4);//写入注册表数据
RegCloseKey(hKey);//关闭注册表键
读注册表:
HKEY hKey;
RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\vcfans\\reg",&hKey);//打开注册表键
DWORD dwType;
DWORD dwValue;
DWORD dwSex;
RegQueryValueEx(hKey,"sex",0,&dwType,(LPBYTE)&dwSex,&dwValue);//查询注册表数据
RegCloseKey(hKey);//关闭注册表键
CString str;
str.Format("sex=%d",dwSex);
MessageBox(str);
相关文章推荐
- Windows编程中各种操作文件的方法
- Windows编程中各种操作文件的方法
- Windows编程中各种操作文件的方法
- Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法
- windows编程中文件操作的几种方法
- sqlserver操作各种文件方法
- windows编程中文件操作各种方法
- Windows编程中各种操作文件的方法
- Windows编程中文件操作有以下几种常见方法
- Windows编程中各种操作文件的方法
- java对文件(file)操作的各种方法
- windows编程中文件操作的几种方法
- 在linux下操作pcap文件的各种方法整理
- Windows编程中各种操作文件的方法
- Windows编程中各种操作文件的方法
- 操作文件和目录的方法
- vbs操作txt文本文件常用方法(函数)
- Python使用ConfigParser模块操作配置文件的方法
- Go语言文件操作的方法
- IOS中获取各种文件的目录路径的方法