[MFC] CFile读写文件实现(高效)
1.文件写入
void CMFCApplication1Dlg::Write()
{
CFile file;
CString FileName = "D:\\100w.txt";
memset(buf, , NUM);//初始化内存,防止读出字符末尾出现乱码
for (int i = ; i < NUM; i++)
{
if ((i / )% == )
buf[i] =;
else
buf[i] = ;
}
// TODO: Add your control notification handler code here
int aa = buf[NUM/];
try
{
file.Open(FileName, CFile::modeCreate | CFile::modeWrite|CFile::typeBinary);
//CArchive ar(&file, CArchive::store);//根据打开的文件,创建文件串行化对象
//Serialize(ar); //写文件内容
//ar.Write(buf, NUM);
////结束后关闭对象
//ar.Close();
file.SeekToBegin();
int len = NUM*sizeof(short int);
file.Write(buf, NUM*sizeof(short int));//CString m_data
//file.Flush();
file.Close();
MessageBox("写入成功!");
}
catch (CFileException *e)
{
CString str;
str.Format("写入失败的原因是:%d", e->m_cause);
MessageBox("str");
file.Abort();
e->Delete();
}
}
2.文件读取
void CMFCApplication1Dlg::Read()
{
CFile file;
CString FileName = "D:\\data.txt";
short int buf[];//读1K
memset(buf, , *sizeof(short int));//初始化内存,防止读出字符末尾出现乱码
try
{
if (!file.Open(FileName, CFile::modeRead))
{
MessageBox("没有文件!");
return;
}
file.Seek( * sizeof(short int), CFile::begin); //结构体格式读取
//DATAS aa;
//file.Read(&aa, sizeof(aa));
file.Read(buf, sizeof(buf));
file.Close();
MessageBox("读出成功!");
}
catch (CFileException *e)
{
CString str;
str.Format("读取数据失败的原因是:%d", e->m_cause);
MessageBox("str");
file.Abort();
e->Delete();
}
}
3.文件的查找
当对一个文件操作时,如果不知道该文件是否存在,就要首先进行查找。MFC中有一个专门用来进行文件查找的类CFileFind,使用它可以方便快捷地进行文件的查找。下面这段代码演示了这个类的最基本使用方法。
CString strFileTitle;
CFileFind finder;
BOOL bWorking = finder.FindFile("C:\\windows\\sysbkup\\*.cab");
4.项目用到的分页读取
读:
void CWave::ShowByPaging_W(CString strPath, int nPageNum, bool bIsShowI, bool bIsShowQ)
{
CFile file;
int nReadDataCount = ;
memset(data_W, , nDataNumOfPage_W* * sizeof(short int));
try
{
if (!file.Open(strPath, CFile::modeRead))
{
MessageBox("没有文件!");
return;
}
file.Seek((nPageNum-) *nDataNumOfPage_W**sizeof(short int), CFile::begin); //返回实际读到参数。若读出数据小于20W,则表示已到末尾;此时读的是字节数
int nReadChar = file.Read(data_W, sizeof(short int)**nDataNumOfPage_W);
nReadDataCount = nReadChar / / sizeof(short int);
//file.Read(buf, sizeof(buf));
file.Close();
}
catch (CFileException *e)
{
//CString str;
//str.Format("读取数据失败的原因是:%d", e->m_cause);
//MessageBox("str");
file.Abort();
e->Delete();
}
int startLoc = nDataNumOfPage_W*(m_nCurPageNum_W-);
int count = ;
for (int i = ; i <nDataNumOfPage_W * ; i++)
{
if (i % == )
data_WQ[i / ] = data_W[i] * m_DeltaY_W;
else
data_WI[i / ] = data_W[i] * m_DeltaY_W;
if (i % == )
{
count++;
data_WX[i / ] = (startLoc+count)* m_DeltaX_W;
}
} LoadData_W(nDataNumOfPage_W, data_WX, data_WI, data_WQ, bIsShowI, bIsShowQ);
}
写:
bool writeFileByData(CString strFileName, UINT16 buf[], int len)
{
string strPath = m_strSavePath_trans+"\\" + strFileName + ".bin"; CFile file;
try
{
//CFile::modeCreate 创建方式打开文件,如文件已存在则将其长度设置为0
//CFile::modeNoTruncate 创建文件时如文件已存在不对其进行截断
//CFile::modeRead 只读方式打开文件
//CFile::modeReadWrite 读写方式打开文件
//CFile::modeWrite 写入方式打开文件
//CFile::typeBinary 设置文件为二进制模式
//CFile::typeText 设置文件为文本模式
file.Open(strPath.c_str(), CFile::modeCreate|CFile::modeNoTruncate | CFile::modeWrite | CFile::typeBinary);
//CArchive ar(&file, CArchive::store);//根据打开的文件,创建文件串行化对象
//Serialize(ar); //写文件内容
//ar.Write(buf, NUM);
////结束后关闭对象
//ar.Close();
file.SeekToEnd();
file.Write(buf, len*sizeof(UINT16));//CString m_data
//file.Flush();
file.Close();
return true;
}
catch (CFileException *e)
{
CString str;
str.Format("写入失败的原因是:%d", e->m_cause);
//MessageBox("str");
file.Abort();
e->Delete();
return false;
}
return false;
}
1. 文件模式标志 说明
CFile::modeCreate 创建方式打开文件,如文件已存在则将其长度设置为0
CFile::modeNoInherit 不允许继承
CFile::modeNoTruncate 创建文件时如文件已存在不对其进行截断
CFile::modeRead 只读方式打开文件
CFile::modeReadWrite 读写方式打开文件
CFile::modeWrite 写入方式打开文件
CFile::shareCompat 在使用过程中允许其他进程同时打开文件
CFile::shareDenyNone 在使用过程中允许其他进程对文件进行读写
CFile::shareDenyRead 在使用过程中不允许其他进程对文件进行读取
CFile::shareDenyWrite 在使用过程中不允许其他进程对文件进行写入
CFile::shareExclusive 取消对其他进程的所有访问
CFile::typeBinary 设置文件为二进制模式
CFile::typeText 设置文件为文本模式
参考链接:
http://www.jizhuomi.com/software/234.html
http://www.jizhuomi.com/software/497.html
http://blog.sina.com.cn/s/blog_69b9bb050100lelo.html
http://blog.csdn.net/iscassucess/article/details/8210069
[MFC] CFile读写文件实现(高效)的更多相关文章
- MFC CFile类读写文件详解
CFile类提供了对文件进行打开,关闭,读,写,删除,重命名以及获取文件信息等文件操作的基本功能,足以处理任意类型的文件操作. 一个读写文件的例子: 文件I/O 虽然使用CArchive类内建的序列化 ...
- MFC读写文件详解
1.CFile类提供了对文件进行打开,关闭,读,写,删除,重命名以及获取文件信息等文件操作的基本功能,足以处理任意类型的文件操作. 虽然使用CArchive类内建的序列化功能是保存和加载持久性数据的便 ...
- c# 高效读写文件
一.同步读写文件(在并发情况下不会发生文件被占用异常) static void Main(string[] args) { Parallel.For(0, 10000, e => { strin ...
- RandomAccessFile类——高效快捷地读写文件
RandomAceessFile类 RandomAccessFile类是一个专门读写文件的类,封装了基本的IO流,在读写文件内容方面比常规IO流更方便.更灵活.但也仅限于读写文件,无法像IO流一样,可 ...
- VC++读写文件
目录 第1章读写文件 1 1.1 API 1 1.2 低级IO 1 1.2.1 文件序号 1 1.2.2 文本文件与二进制文件 1 1.3 流IO 2 1.4 Un ...
- 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库
57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...
- Qt的Model/View Framework解析(数据是从真正的“肉(raw)”里取得,Model提供肉,所以读写文件、操作数据库、网络通讯等一系列与数据打交道的工作就在model中做了)
最近在看Qt的Model/View Framework,在网上搜了搜,好像中文的除了几篇翻译没有什么有价值的文章.E文的除了Qt的官方介绍,其它文章也很少.看到一个老外在blog中写道Model/Vi ...
- Java开发笔记(八十六)通过缓冲区读写文件
前面介绍了利用文件写入器和文件读取器来读写文件,因为FileWriter与FileReader读写的数据以字符为单位,所以这种读写文件的方式被称作“字符流I/O”,其中字母I代表输入Input,字母O ...
- Java编程的逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
随机推荐
- Spring中的事务传播行为与隔离级别
事务传播行为 事务传播行为(为了解决业务层方法之间互相调用的事务问题): 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播.例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己 ...
- 特别困的学生 UVa12108(模拟题)
一.题目 课堂上有n个学生(n<=10).每个学生都有一个“睡眠-清醒”周期,其中第i个学生醒Ai分钟后睡Bi分钟,然后重复(1<=Ai,Bi<=5),初始第i个同学处于他的周期的C ...
- 两个input标签之间间隙问题的解决
<input type="text"> <input type="button" value="搜索"> 代码显示效 ...
- 稳定性 耗时 gc 过长问题排查 和工具
自己的另外一篇: http://www.cnblogs.com/fei33423/p/7805186.html 偶有耗时抖动? gc 也有长耗时? fullgc 也是? 有同学反馈 swap 可能导致 ...
- Java中的日期(Calendar、Date)
(1)获取当前日期: java.util.Calendar calendar = java.util.Calendar.getInstance(); 或 = new java.util.Gregor ...
- Lucene入门基础教程
http://www.linuxidc.com/Linux/2014-06/102856.htm
- 一. python基础知识
第一章.变量与判断语句 1.第一个python程序 # -*- coding:utf-8 -*- # Author: Raymond print ("hello world") p ...
- docker的网络(进阶)
overlay网络 overlay网络驱动程序会在多个docker守护程序(即多个主机上的docker守护程序)之间创建分布式网络.该网络(overlays)位于特定于主机的网络之上,允许连接到它的容 ...
- a标签中javascript和void
<body> <a href="javascript:;">点了无反应</a> <a href="javascript:void ...
- 气泡小角的css实现
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> ...