文件读写学习笔记
2005-08-16 20:53
507 查看
原创 我的网址 http://blog.csdn.net/hsuyuan/ 欢迎探讨学习一.写入文件
C语言中的写入
FILE *pFile=fopen("1.text","w");
fwrite("http://www.sina.com.cn",1,strlem(http://www.sina.com.cn),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("http://www.sina.com.cn",strlen("http://www.sina.com.cn"));
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,"http://sina.com.cn",strlen("http://sina.com.cn"),&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语言中的写入
FILE *pFile=fopen("1.text","w");
fwrite("http://www.sina.com.cn",1,strlem(http://www.sina.com.cn),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("http://www.sina.com.cn",strlen("http://www.sina.com.cn"));
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,"http://sina.com.cn",strlen("http://sina.com.cn"),&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);
相关文章推荐
- Python学习笔记(3)--数据文件的读写
- 【MFC 学习笔记】CFile读写文件
- 【perl】学习笔记(五)--文件读写
- 学习笔记 安卓常用的XML文件读写方式--pull,Dom,SAX
- Unix学习笔记------文件IO------使用lseek设置文件当前读写偏移量(即:当前的读写位置)
- 文件读写学习笔记
- Java学习笔记——File类文件管理及IO读写、复制操作
- PHP学习笔记——文件读写
- 学习笔记-C语言-文件读写
- PHP文件读写操作相关函数学习资料笔记
- python学习笔记之编写readConfig读写配置文件
- OpenCV学习笔记 cv.Mat 与 .txt 文件数据的读写操作
- Androidx学习笔记(12)-- 文件读写操作-在外部存储空间中读写文件
- Python学习笔记(五)——读写文件
- Lua学习笔记8:文件读写
- Python3学习笔记7-文件的读写
- 初学Python的学习笔记12----单元测试、文档测试和文件读写
- QT之GUI学习笔记---二进制文件读写
- BMP文件学习笔记(一): C/C++语言实现.bmp文件读写
- Linux学习笔记2-文件读写操作