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. appdomain概念与应用

    用appcmd命令显示w3wp.exe进程,很多时候一个应用程序池负责创建一个w3wp.exe进程与之对应,可能还有时候是创建多个w3wp.exe,有待验证. c:\Windows\System32\ ...

  2. SQL-W3School-高级:SQL UNIQUE 约束

    ylbtech-SQL-W3School-高级:SQL UNIQUE 约束 1.返回顶部 1. SQL UNIQUE 约束 UNIQUE 约束唯一标识数据库表中的每条记录. UNIQUE 和 PRIM ...

  3. MongoDB安装成windows 服务

    观看本文之间,请先移步至下面纠正部分 之前按照MongoDB官网提供的安装方法一直出错 http://cn.docs.mongodb.org/master/tutorial/install-mongo ...

  4. js取url问号后的参数方法封装

    工具方法: function getRequest() { var url = location.search; // 获取url中?后面的字符串 var theRequest = new Objec ...

  5. k8s install kubeadm网络原因访问不了谷哥and gpg: no valid OpenPGP data found. 解决办法

    gpg: no valid OpenPGP data found. 解决办法 待做.................................... 卡助在这curl -s https://pa ...

  6. MUI和html5plus

    MUI: http://dev.dcloud.net.cn/mui/snippet/ html5plus: http://www.html5plus.org/doc/h5p.html

  7. Linux磁盘和文件系统扩容彻底研究

    1.物理卷: LVM 逻辑卷的底层物理存储单元是一个块设备,比如一个分区或整个磁盘.要在 LVM 逻辑卷中使用该设备,则必须将该设备初始化为物理卷(PV). 2.卷组:物理卷合并为卷组(VG).这样就 ...

  8. 外带IP 防火墙限制导致 IP不通

    案例: 业务报障,一台设备配了20个IP,跳板机测试都通,但从外边访问,发现部分IP通,部分不通. 排雷: 1. 从跳板机测试都通,说明所有IP 本身应该都没问题的,都可以用,2. 从其他设备测试,部 ...

  9. 《Java语言程序设计》继承与多态

    一.动手实验:继承条件下的构造方法 调用运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent ...

  10. 【Qt开发】 QT:make: Nothing to be done for `first'和error:QtSql:No such file or directory

    http://blog.csdn.NET/heqiuya/article/details/7774208 这是QT编程中常见的两个编译错误.可能你的代码在window下编译能正常通过,可是到到Linu ...