std::string与char*之临时缓冲区

原文:https://blog.csdn.net/hsshh1988/article/details/80689330

c++文件读取流程如下:

ifstream ifs(srcFile, ifstream::binary);
if(ifs.is_open())
{
ifs.seekg(, ifs.end);
long filesize = ifs.tellg();
ifs.seekg (); char* fileBuffer = new char[filesize]; //分配内存缓冲区
ifs.read(fileBuffer, filesize); //读取文件内容到缓冲区
ifs.close(); //do sth. with fileBuffer delete []fileBuffer; //release memory
}

整个流程中内存的分配和读没什么问题。如果需要对fileBuffer的内容做些字符串处理相关的工作,因std::string操作比较方便,通常会先把fileBuffer转化为std::string,然后再删掉fileBuffer,如下

char* fileBuffer = new char[filesize]; //分配内存缓冲区
ifs.read(fileBuffer, filesize); //读取文件内容到缓冲区
ifs.close(); std::string strBuffer(fileBuffer);
delete []fileBuffer; //release memory //do sth. with strBuffer...

这么做其实也没什么问题,风险无非有两个,一是忘了delete []fileBuffer,二是内存缓冲区可能在创建strBuffer时翻了一倍,虽然std::string在设计时考虑到了copy on write,但风险依然是存在的。

为了解决上面若有若无的风险,需要使用c++程序设计中常用的模式RAII即“资源获取就是初始化”,用对象来管理资源,如下:

std::string strBuffer(filesize, );//分配内存缓冲区
char* fileBuffer = &*strBuffer.begin(); //获取分配内存缓冲区的首地址 ifs.read(fileBuffer, filesize); //读取内容到缓冲区
ifs.close(); //do sth. with strBuffer or fileBuffer...

另外,C++11后调整了std::string,收紧了相关权限,比如:

常量字符串必须是const只读的

char* str = "this is test string";  //wrong
const char* str = "this is a test string"; //right

const char* 对象不能赋值给 char*

std::string str = "this is test string";
char* szStr = str.c_str(); //wrong
char* szStr = str.data(); //wrong
const char* szStr = str.c_str(); //right
const char* szStr = str.data(); //right

如果需要把std::string赋值给char*,即取得std::string对象的可读写首地址,需要转变思路

先获取首元素,然后对其取地址

std::string str = "this is a string";
char* szStr = &*str.begin();
char* szStr = &str[];

这样对szStr的操作也对str生效了。

==================== End

std::string与char*之临时缓冲区的更多相关文章

  1. C++ 将 std::string 转换为 char*

    参考: std::string to char* C++ 将 std::string 转换为 char* 目前没有直接进行转换的方法.必须通过string对象的c_str()方法,获取C-style的 ...

  2. 高德地图引入库错误std::string::find_first_of(char const*, unsigned long, unsigned long) const"

    一:std:编译器错误解决 二:错误提示 "std::string::find_first_of(char const*, unsigned long, unsigned long) con ...

  3. C++ std::unordered_map使用std::string和char *作key对比

    最近在给自己的服务器框架加上统计信息,其中一项就是统计创建的对象数,以及当前还存在的对象数,那么自然以对象名字作key.但写着写着,忽然纠结是用std::string还是const char *作ke ...

  4. QString, Std::string, char *相互转换

    Qt 库中对字符串类型进行了封装,QString 类提供了所有字符串操作方法,给开发带来了便利. 由于第三方库的类型基本上都是标准的类型,即使用std::string或char *来表示字符 (串) ...

  5. How to convert a std::string to const char* or char*?

    How to convert a std::string to const char* or char*? 1. If you just want to pass a std::string to a ...

  6. C++ 字符串、string、char *、char[]、const char*的转换和区别

    1.字符串 字符串本质就是一串字符,在C++中大家想到字符串往往第一反应是std::string(后面简称string) 字符串得从C语言说起,string其实是个类,C语言是没有class的,所以C ...

  7. c++ istream转换为std::string

    std::istreambuf_iterator<char> eos; std::string s(std::istreambuf_iterator<char>(stream) ...

  8. c++之常见数据类型(int,double,float,long double long long 与std::string之间)相互转换(含MFC的CString、含C++11新特性函数)

    --- 已经通过初步测试---- ------------------ 下面的是传统常见数据类型的转换(非c++11)---------------  std::string 与其他常用类型相互转换, ...

  9. std::string stringf(const char* format, ...)

    std::string stringf(const char* format, ...){ va_list arg_list; va_start(arg_list, format); // SUSv2 ...

随机推荐

  1. sqlserver 锁表进程及执行的SQL

    --1.查进程select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableNamefrom sys.d ...

  2. FIREDAC返回多结果集

    FIREDAC返回多结果集 以前使用ADO, 如果SQL返回的结果集有多个 可以通过NextRecordset来依次获取 代码移植到FireDAC, 对于多结果集处理差不多, 但是还是有一些不一样的地 ...

  3. Qt打开文件QFileDialog

    //打开Pts文件按钮点击事件void AnalysisPtsDataTool201905::OnOpenFileButtonClick(){ qDebug()<<"open f ...

  4. setInterval、clearInterval的回调函数,实现函数间调用的先后顺序

    定义: var waitUnitil=function (untillCallBack, nextStepCallBack, count) { if (count == null) { count = ...

  5. jExcelAPI 操作 Excel 文件

    在开源世界中,有两套比较有影响的API可 供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一点.但jExcelAPI对中文支持非常好,API是纯Java的, 并不 依赖Wind ...

  6. 解析Python编程中的包结构

    解析Python编程中的包结构 假设你想设计一个模块集(也就是一个"包")来统一处理声音文件和声音数据.通常由它们的扩展有不同的声音格式,例如:WAV,AIFF,AU),所以你可能 ...

  7. Python 标准库之 fcntl

    在 linux 环境下用 Python 进行项目开发过程中经常会遇到多个进程对同一个文件进行读写问题,而此时就要对文件进行加锁控制,在 Python 的 linux 版本下有个 fcntl 模块可以方 ...

  8. 反向代理远端 单台tomcat 使用域名代理

    .环境 nginx 10.1.1.161 公网:123.58.251.166 tomcat 10.1.1.103 .远端tomcat 配置 [root@host---- ~]# netstat -tn ...

  9. JDBC获得DB2表结构并且将表中数据脱敏后转移的程序示例

    完整项目地址:https://github.com/zifeiy/totomi 代码示例: import java.io.File; import java.io.FileInputStream; i ...

  10. bash小结

    context:CentOS 什么是shell? shell就是与计算机交互的接口. linux支持的shell [root@node1 ~]# cat /etc/shells /bin/sh #被 ...