文件读写的操作整理
2014-07-13 17:50
357 查看
一.写入文件
由于文件操作平时经常要用到,所以将一些常用的文件操作整理如下,其中包括C语言中文件读写的应用,c++中文件读写的应用,SDK中文件读写的应用和MFC中文件读写的应用。
C语言中的写入
FILE *pFile=fopen("1.text","w");
fwrite("wenjianduxie“,1,strlen(wenjianduxie),pFile);
fclose(pFile);//关闭 把文件写入缓冲区
fflush(pFile);//刷新缓冲区,立即更新
移动到开头写入数据
移动指针
fseek()函数实现
fseek(pFile,0,SEEK_SET);
方法2:
FILE *pFile=fopen("2.text","w");
char ch[3];
ch[0]='a';
ch[1]=10;
ch[2]='b';
fweite(ch,1,3,pFile);
fclose(pFile);
//产生的文件多了一个字节
打开方式中加"b"将按二进制打开
C++中的写入
ofstream ofs("4.text");
ofs.weite("wenjianduxie",strlen("wenjianduxie"));
ofs.close();
//需要包含头文件:#include "fstream.h"
SDK中对文件的写入打开
CreateFile类 返回句柄
参数(文件名 ,访问方式,共享方式,指向结构的指针,如何创建,设置文件属性,指定模板文件句柄)
HANDLE hFile;
hFile=CreateFile("5.text",GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dvWrites;
WriteFile(hfile,"wenjianduxie",strlen("wenjianduxie"),&dvWrites,NULL);
CloseHandle(hFile);
MFC中对文件的写
CFile file("6.text",CFile::modeCreate|CFile::modeWrite);
file.Write("http://sina.com.cn",strlen("http://sina.com.cn"));
file.Close();
创建一个写入文件对话框
使用类 CFileDialog
CFileDialog filedlg(FALSE);
filedlg.m_ofm.IpstrTile="我的文件";//改变标题
filedlg.m_ofm.Ipstrfilter="Text files(*.text)/0*.text/0ALL Files(*.*)/0*.*/0/0";//设置过滤器
filedlg.m_ofm.IpstrDefExt="tet";//添加缺省扩展名
if (IDOK==filedlg.DoModal());
{
CFile file(filedlg.GetfileName(),CFile::modeCreate|CFile::modeWrite);
file.Write("http://sina.com.cn",strlen("http://sina.com.cn"));
file.Close();
}
初始化程序的写入 对INI文件的写(针对16位的操作系统在32位操作系统中因该对注册表进行读写)
使用类WriteProfileString
::WriteProfileString("段名","keyname","string");
二.读取文件
C语言中的读取
FILE *pFile=fopen("1.text","r")
char ch[100];
/*
menset(ch,0,100);//全部设为0
*/
fread(ch,1,100,pFile);//通过写入数据是把strlem()+1实现加上/0
MessageBox(ch);
fclose(pFile);
/*
ftell(FILE *stream)//得到文件当前的位置
*/
char *pBuf;
fseek(pFile,0,SEEK_END);
int len=ftell(pFile);
pBuf=new char[len+1];
rewind(pFile);//指针移到文件头
fread(pBuf,1,len,pFile);
pBuf[len]=0;
MessageBox(pBuf);
//得到文件长度输出数据
方法2:
FILE *pFile=fopen("2.text","r");
char ch[100];
fread(ch,1,3,pFile);
ch[3]=0;
MessageBox(ch);
fclose(pFile);
用二进制方式读时 字节和文本方式读不同
例子:保存整形数据98241在文本文件中,当打开文件时文本显示为98241
分析:文本保存的是ASC2码的数据,不是一个数值,一个int占四个字节
程序:
FILE *pFile=fopen("3.text","w");
int i=98341;
fwrite(&i,4,1,pFile);
fclose(pFile);
以上程序打开的是乱码
0 -- 48 ASC
FILE *pFile=fopen("3.text","w");
int i=98341;
char ch[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);
fwrite(ch,1,5,pFile);
fclose(pFile);
C++中的读出
使用ifstream类
ifstream ifs("4.text")
char ch[100];
menset(ch,0,100);
ifs.read(ch,100);
ifs.close();
MessageBox(ch);
SDK中对文件的打开读写
HANDLE hFile;
hFile=CreateFile("5.text",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
char ch[100];
DWORD dvReads;
ReadFile(hFile,cyh,100,&dvReads,NULL);
ch[dvReads]=0;
CloseHandle(hFile);
MessageBoxe(ch);
MFC中对文件的读
CFile file("6.text",CFile::modeRead);
char *pBuf;
DWORD dvFile;
deFile=file.GetLength();
pBuf=new char[dvFile+1];
pBuf[dvFile]=0;
file.Read(pBuf,dufile);
file.close();
创建一个读出文件对话框
使用类 CFileDialog
CFileDialog filedlg(FALSE);
filedlg.m_ofm.IpstrTile="我的文件";//改变标题
filedlg.m_ofm.Ipstrfilter="Text files(*.text)/0*.text/0ALL Files(*.*)/0*.*/0/0";//设置过滤器
filedlg.m_ofm.IpstrDefExt="tet";//添加缺省扩展名
if (IDOK==filedlg.DoModal());
{
CFile file(filedlg.GetfileName(),CFile::modeRead);
char *pBuf;
DWORD dvFile;
deFile=file.GetLength();
pBuf=new char[dvFile+1];
pBuf[dvFile]=0;
file.Read(pBuf,dufile);
file.close();
MessageBox(pBuf)
}
初始化程序的读 对INI文件的读(针对16位的操作系统在32位操作系统中因该对注册表进行读写)
使用类GetProfileString
CString str;
::GetProfileString("段名","keyname","缺省字符串",str.GetBuffer(100),100);
AFXMessageBox(str);
由于文件操作平时经常要用到,所以将一些常用的文件操作整理如下,其中包括C语言中文件读写的应用,c++中文件读写的应用,SDK中文件读写的应用和MFC中文件读写的应用。
C语言中的写入
FILE *pFile=fopen("1.text","w");
fwrite("wenjianduxie“,1,strlen(wenjianduxie),pFile);
fclose(pFile);//关闭 把文件写入缓冲区
fflush(pFile);//刷新缓冲区,立即更新
移动到开头写入数据
移动指针
fseek()函数实现
fseek(pFile,0,SEEK_SET);
方法2:
FILE *pFile=fopen("2.text","w");
char ch[3];
ch[0]='a';
ch[1]=10;
ch[2]='b';
fweite(ch,1,3,pFile);
fclose(pFile);
//产生的文件多了一个字节
打开方式中加"b"将按二进制打开
C++中的写入
ofstream ofs("4.text");
ofs.weite("wenjianduxie",strlen("wenjianduxie"));
ofs.close();
//需要包含头文件:#include "fstream.h"
SDK中对文件的写入打开
CreateFile类 返回句柄
参数(文件名 ,访问方式,共享方式,指向结构的指针,如何创建,设置文件属性,指定模板文件句柄)
HANDLE hFile;
hFile=CreateFile("5.text",GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dvWrites;
WriteFile(hfile,"wenjianduxie",strlen("wenjianduxie"),&dvWrites,NULL);
CloseHandle(hFile);
MFC中对文件的写
CFile file("6.text",CFile::modeCreate|CFile::modeWrite);
file.Write("http://sina.com.cn",strlen("http://sina.com.cn"));
file.Close();
创建一个写入文件对话框
使用类 CFileDialog
CFileDialog filedlg(FALSE);
filedlg.m_ofm.IpstrTile="我的文件";//改变标题
filedlg.m_ofm.Ipstrfilter="Text files(*.text)/0*.text/0ALL Files(*.*)/0*.*/0/0";//设置过滤器
filedlg.m_ofm.IpstrDefExt="tet";//添加缺省扩展名
if (IDOK==filedlg.DoModal());
{
CFile file(filedlg.GetfileName(),CFile::modeCreate|CFile::modeWrite);
file.Write("http://sina.com.cn",strlen("http://sina.com.cn"));
file.Close();
}
初始化程序的写入 对INI文件的写(针对16位的操作系统在32位操作系统中因该对注册表进行读写)
使用类WriteProfileString
::WriteProfileString("段名","keyname","string");
二.读取文件
C语言中的读取
FILE *pFile=fopen("1.text","r")
char ch[100];
/*
menset(ch,0,100);//全部设为0
*/
fread(ch,1,100,pFile);//通过写入数据是把strlem()+1实现加上/0
MessageBox(ch);
fclose(pFile);
/*
ftell(FILE *stream)//得到文件当前的位置
*/
char *pBuf;
fseek(pFile,0,SEEK_END);
int len=ftell(pFile);
pBuf=new char[len+1];
rewind(pFile);//指针移到文件头
fread(pBuf,1,len,pFile);
pBuf[len]=0;
MessageBox(pBuf);
//得到文件长度输出数据
方法2:
FILE *pFile=fopen("2.text","r");
char ch[100];
fread(ch,1,3,pFile);
ch[3]=0;
MessageBox(ch);
fclose(pFile);
用二进制方式读时 字节和文本方式读不同
例子:保存整形数据98241在文本文件中,当打开文件时文本显示为98241
分析:文本保存的是ASC2码的数据,不是一个数值,一个int占四个字节
程序:
FILE *pFile=fopen("3.text","w");
int i=98341;
fwrite(&i,4,1,pFile);
fclose(pFile);
以上程序打开的是乱码
0 -- 48 ASC
FILE *pFile=fopen("3.text","w");
int i=98341;
char ch[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);
fwrite(ch,1,5,pFile);
fclose(pFile);
C++中的读出
使用ifstream类
ifstream ifs("4.text")
char ch[100];
menset(ch,0,100);
ifs.read(ch,100);
ifs.close();
MessageBox(ch);
SDK中对文件的打开读写
HANDLE hFile;
hFile=CreateFile("5.text",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
char ch[100];
DWORD dvReads;
ReadFile(hFile,cyh,100,&dvReads,NULL);
ch[dvReads]=0;
CloseHandle(hFile);
MessageBoxe(ch);
MFC中对文件的读
CFile file("6.text",CFile::modeRead);
char *pBuf;
DWORD dvFile;
deFile=file.GetLength();
pBuf=new char[dvFile+1];
pBuf[dvFile]=0;
file.Read(pBuf,dufile);
file.close();
创建一个读出文件对话框
使用类 CFileDialog
CFileDialog filedlg(FALSE);
filedlg.m_ofm.IpstrTile="我的文件";//改变标题
filedlg.m_ofm.Ipstrfilter="Text files(*.text)/0*.text/0ALL Files(*.*)/0*.*/0/0";//设置过滤器
filedlg.m_ofm.IpstrDefExt="tet";//添加缺省扩展名
if (IDOK==filedlg.DoModal());
{
CFile file(filedlg.GetfileName(),CFile::modeRead);
char *pBuf;
DWORD dvFile;
deFile=file.GetLength();
pBuf=new char[dvFile+1];
pBuf[dvFile]=0;
file.Read(pBuf,dufile);
file.close();
MessageBox(pBuf)
}
初始化程序的读 对INI文件的读(针对16位的操作系统在32位操作系统中因该对注册表进行读写)
使用类GetProfileString
CString str;
::GetProfileString("段名","keyname","缺省字符串",str.GetBuffer(100),100);
AFXMessageBox(str);
相关文章推荐
- 整理javascript操作文件说明:读写文件
- C++ builder 的文件读写操作总结 (来源:论坛整理 作者:佚名)
- 整理javascript操作文件说明:读写文件
- 文件读写操作工具类整理
- C# 文件读写操作整理
- C语言进阶-第36~37讲:随机读写&二进制文件&文件操作整理
- 一个非常非常非常基础的程序,写的不好,但是一般的文件读写操作及字符处理函数都涉及到了..新手学习用的
- 市面上所有号称"虚拟机","防火墙"的实时监控杀毒软件无一不是使用的IFSHOOK技术.但是同时也有一些朋友不断写MAIL给我打听如何实现读写的监控.下面给出用VTOOLSD写的代码.也就是所有实时杀毒软件的奥秘.同时,很多拦截文件操作的软件,例如对目录加
- 文件读写操作
- lr中读写文件操作代码(原创)
- 另一个文件读写操作
- 对于大文件的读写操作
- 对项目配置文件app.comfig进行读写操作
- java文件读写的相关类整理(from jdk)
- 文件读写操作
- 文件随机读写操作
- 对WEB.Config文件进行(读写操作)
- VB 写的二进制文件读写数据库操作模块
- 第二十三课:PHP中文件读写操作
- 含读写及替换字符的复制文件操作