1.C
语言中文件操作。
2.C++语言中的文件操作。
3.Win32 API函数文件操作。
4.MFC CFile类文件操作。
5.MFC CFileDialog类的文件操作。
6.注册表文件操作。
6. 注册表文件操作。
写入注册表:
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);
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);// 关闭文件
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);// 关闭文件
2.C++
语言中的文件操作。需要包含的头文件
fstream.h
写入文件:
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);// 输出结果
写入文件:
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
写入文件:
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);// 输出结果
写入文件:
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
写入文件:
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);// 输出数据
写入文件:
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
写入文件:
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);
};
写入文件:
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. 注册表文件操作。
写入注册表:
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);
//
以上代码在
VC6.0
,
Windows 2K server
下编译通过
在Visual C++中使用fopen()函数来读写文件
|
添加记录: 类别: VC语言 发布日期: 01月01日
|
在Visual C++中使用fopen()函数来读写文件 刘志荣 很多使用过Turbo C的朋友喜欢用fopen()函数来读写文件,在Visual C++环境中您也可以使用fopen()来读写文件。而且您还可以使用Visual C++中的标准的打开文件对话框,这样可以使选取读写文件的路径更方便。具体的做法是这样的。 首先您需要使用到MFC基本类库,在您原有的Visual C++程序中加上一个由CFileDialog类派生的新类,例如是CMyFileDialog,然后用这个新类定义一个对象,例如是:myDlg,使用这个对象的DoModal()方法,就可以呼出打开文件的对话框。这样,您只要把被选中的路径名及文件名传送给fopen()函数就可以打开读写这个文件了。 CMyFileDialog myDlg(true); 定义一个对话框对象 myDlg.DoModal(); 呼出打开文件对话框 CString fileName; 定义一个Cstring类型的字符串来存储文件的路径及文件名 fileName=myDlg.GetPathName(); 得到文件的路径及文件名 由于fopen()函数中需要的文件名参数是字符型的,所以我们还需要把CString类型的文件名转换为字符类型的变量。 int i, length; 定义i作为循环变量,定义length作为文件路径及文件名的长度 Char* FileName; 定义char型的字符指针来存储文件名,给fopen()函数使用 length=fileName.GetLength(); 把文件名的长度赋给length变量 FileName=new char[length]; 给char型的指针变量FileName分配内存空间 for(i=0;i<length;i++) 把CString型变量fileName中的字符一个一个地赋给char型的指针变量FileName *(FileName+i)=fileName.GetAt(i); *(FileName+length)=NULL; 在FileName变量的最后加上一个结束标志 这样,您就要吧使用fopen(FileName,"w")来写文件,或者是用fopen(FileName,"r")来读文件了。FileName就是使用打开文件对话框得到的文件路径及文件名。 |