文件读写学习笔记

一.写入文件
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);

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值