关于c++对文件读写的封装
namespace
{
UINT_T GetWriteSizeForNoBuf(UINT_T fsize)
{
UINT_T write_buf_size = ;
if (fsize % == )
{
write_buf_size = fsize;
}
else
{
write_buf_size = (fsize / + )* ;
}
return write_buf_size;
} } //将文件解析成路径,名称,类型
bool CFileBak::ParseFilePath(const string& file_path_name, string& file_path, string& file_name, string& file_type)
{
size_t path_pos = file_path_name.find_last_of("/\\"); // 考虑/,\两种表示路径的方法
if (path_pos == string::npos)
{
assert();
return false;
}
file_path = file_path_name.substr(, path_pos);
file_name = file_path_name.substr(path_pos + );
size_t type_pos = file_name.find_last_of(".");
if (type_pos != string::npos)
{
file_type = file_name.substr(type_pos + );
file_name = file_name.substr(, type_pos);
}
return true;
} bool CFileBak::ReadFile(std::string& buf, const std::string& file_path_name, const std::string& bak_name_tmp)
{
bool need_bak = true;
FILE* ptr_file = NULL;
size_t fsize = ;
string bak_name = bak_name_tmp;
bool tmp_false = false;
do
{
fopen_s(&ptr_file, file_path_name.c_str(), "rb");
if (NULL == ptr_file)
{
break;
} //get file size
fseek(ptr_file, , SEEK_END);
fsize = (ftell(ptr_file));
fseek(ptr_file, , SEEK_SET);
if (fsize == )
{
fclose(ptr_file);
ptr_file = NULL;
break;
}
need_bak = false;
} while (tmp_false); if (need_bak)
{
//assert(0); // 已经属于异常范畴了
if (bak_name.empty())
{
string file_path;
string file_name;
string file_type;
if (!ParseFilePath(file_path_name, file_path, file_name, file_type))
{
assert();
return false;
}
bak_name = file_path + "/" + file_name + ".data";
} fopen_s(&ptr_file, bak_name.c_str(), "rb");
if (NULL == ptr_file)
{
//assert(0);
return false;
} //get file size
fseek(ptr_file, , SEEK_END);
fsize = (ftell(ptr_file));
fseek(ptr_file, , SEEK_SET);
}
if (nullptr == ptr_file)
{
assert();
return false;
} if ( == fsize)
{
assert();
fclose(ptr_file);
return false;
} auto_ptr<char> pBuf(new char[fsize]);
memset(pBuf.get(), , fsize);
size_t read_size = fread(pBuf.get(), , fsize, ptr_file);
fclose(ptr_file);
buf.assign(pBuf.get(), read_size);
//buf = string(pBuf.get(), read_size); size_t n_pos = buf.find_last_not_of('\n');
if (string::npos != n_pos)
{
buf = buf.substr(, n_pos + );
}
return true;
} bool CFileBak::WriteFile(const std::string& file_name, const std::string& buf)
{
HANDLE hfile = CreateFileA(file_name.c_str(), GENERIC_WRITE | GENERIC_READ, , , OPEN_ALWAYS, FILE_FLAG_NO_BUFFERING, );
if (INVALID_HANDLE_VALUE == hfile)
{
assert();
CloseHandle(hfile);
return false;
} LARGE_INTEGER large_file_size;
if (!GetFileSizeEx(hfile, &large_file_size))
{
assert();
CloseHandle(hfile);
return false;
}
size_t n_pos = buf.find_last_not_of('\n'); string write_buf;
if (string::npos != n_pos)
{
write_buf = buf.substr(, n_pos + );
}
else
{
write_buf = buf;
}
UINT_T write_size = GetWriteSizeForNoBuf(max(large_file_size.LowPart, static_cast<UINT_T>(write_buf.length())));
if (write_size == )
{
assert();
CloseHandle(hfile);
return false;
} auto_ptr<char> ptr_write_buf(new char[write_size]);
memset(ptr_write_buf.get(), '\n', write_size); // 将换行写在文件中不影响使用,写0都会影响xml的功能
memcpy(ptr_write_buf.get(), write_buf.c_str(), write_buf.length()); DWORD dwret = ;
::WriteFile(hfile, ptr_write_buf.get(), write_size, &dwret, NULL);
CloseHandle(hfile);
return true;
}
关于c++对文件读写的封装的更多相关文章
- php文件操作(最后进行文件常用函数封装)
文件信息相关API $filename="./1-file.php"; //filetype($filename):获取文件的类型,返回的是文件的类型 echo '文件类型为:', ...
- JS封装插件:实现文件读写功能
scripting.FileSystemObject是一个可以实现文件读写的COM组件,由于COM组件可以被跨语言调用,因此可以选择像vbs或者JS这种脚本语言调用,下面我就使用该COM组件封装了一个 ...
- java 对txt文件读写(已经封装好)
读文件: public static String readTxt(String txtPath) { File file = new File(txtPath); if(file.isFile() ...
- .net学习笔记--文件读写的几种方式
在.net中有很多有用的类库来读写硬盘上的文件 一般比较常用的有: File:1.什么时候使用:当读写件大小不大,同时可以一次性进行读写操作的时候使用 2.不同的方式可以读写文件类型不 ...
- Python之文件读写
本节内容: I/O操作概述 文件读写实现原理与操作步骤 文件打开模式 Python文件操作步骤示例 Python文件读取相关方法 文件读写与字符编码 一.I/O操作概述 I/O在计算机中是指Input ...
- Linux文件读写机制及优化方式
导读 Linux是一个可控性强的,安全高效的操作系统.本文只讨论Linux下文件的读写机制,不涉及不同读取方式如read,fread,cin等的对比,这些读取方式本质上都是调用系统api read,只 ...
- iOS数据持久化文件读写之偏好设置
很多iOS应用都支持偏好设置,比如保存用户名.密码.字体大小等设置,iOS提供了一套标准的解决方案来为应用加入偏好设置功能.每个应用都有个NSUserDefaults实例,通过它来存取偏好设置.比如, ...
- (转)linux文件读写的流程
转自http://hi.baidu.com/_kouu/item/4e9db87580328244ef1e53d0 在<linux内核虚拟文件系统浅析>这篇文章中,我们看到文件是如何被打开 ...
- Linux高级编程--05.文件读写
缓冲I/O和非缓冲I/O 文件读写主要牵涉到了如下五个操作:打开.关闭.读.写.定位.在Linux系统中,提供了两套API, 一套是C标准API:fopen.fclose.fread.fwrite.f ...
随机推荐
- Union Find - 20181102 - 20181105
Union Find: 589. Connecting Graph public class ConnectingGraph { //父节点数组 private int[] father = null ...
- Zabbix触发器函数之count函数
一.背景 zabbix监控中我们用的最多的是count这个函数,通过确认多次可以减少很多误告警,提高了运维效率.可以设置连续几次都异常才发出告警,这样一来,只要发出告警基本上就已经确定发生故障了. 二 ...
- 安装Drupal
我在虚拟机里面安装了Ubuntu Server 14.参考https://www.digitalocean.com/community/tutorials/how-to-install-drupal- ...
- 第三章:ionic环境搭建之windows篇
下面是在windows操作系统上面安装ionic的步骤,已经在Windows 10/ 7/ XP下面通过验证. 安装JDK 1.1 下载(http://www.oracle.com/technetwo ...
- 新手 php连接数据库大概。简单过程浅析以及遇到的问题分析
原文作者:aircraft 原文地址: https://www.cnblogs.com/DOMLX/p/8116845.html 重点:PHP运行在服务器上的请记住!!! 1.在连接数据库与PHP之前 ...
- ife task0003学习笔记(一):JavaScript作用域
在学习JavaScript作用域概念之前,首先要明白几个概念:执行环境.变量对象.作用域链. 一.JavaScript执行环境(execution context): 在<Professiona ...
- Win10内置应用恢复初始状态
和Win8/Win8.1相同,Win10也内置了很多默认Windows应用,比如计算器.天气.人脉.Groove音乐.电影和电视.邮件和日历.Edge浏览器等.一般情况下,这些应用不会有太大的问题,但 ...
- C++程序设计基础(1)程序的编译和执行
注:读<程序员面试笔记>笔记总结 1.编译执行过程 1.1预处理: (1)所有以#开头的代码都属于预处理的范畴:#include,#define,#ifdef(#ifndef,#endif ...
- 打包.NET Core的程序到一个单独的可执行文件
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:打包.NET Core的程序到一个单独的可执行文件.
- Java基础之 学java从宝宝的命令行做起
JAVA学习笔记 JAVA命令行 在当前文件的命令行下 编译:输入命令javac GetGreeting.java 执行 命令 Java GetGreeting 有package包的程序 1.到文件当 ...