ostringstream返回c字符串的问题
时间:2011-12-18
来源:互联网
oss << "GET / HTTP/1.1\r\nHost: www.csdn.net\r\n\r\n";
const char * buf = oss.str().c_str();
cout << buf << endl; //乱码,为什么?
cout << oss.str().c_str(); //可以
作者: jronald 发布时间: 2011-12-18
#include <iostream> #include <sstream> using namespace std; int main() { ostringstream oss; oss << "GET / HTTP/1.1\r\nHost: www.csdn.net\r\n\r\n"; char *buf = new char[oss.str().length() + 1]; strcpy(buf , oss.str().c_str()); cout << buf << endl; //乱码,为什么? cout << oss.str().c_str(); //可以 return 0; }
作者: hnuqinhuan 发布时间: 2011-12-18
作者: hnuqinhuan 发布时间: 2011-12-18
const value_type *c_str( ) const;
Return Value:
A pointer to the C-style version of the invoking string. The pointer value is not valid after calling a non-const function, including the destructor, in the basic_string class on the object.
作者: yaningfan 发布时间: 2011-12-18
作者: yaningfan 发布时间: 2011-12-18
#include<iostream> #include <string> #include <sstream> using std::cout; using std::endl; using std::ostringstream; using std::string; int main() { ostringstream oss; oss << "GET / HTTP/1.1\nHost: www.csdn.net"; string str(oss.str()); const char *buf = str.c_str(); cout << buf << endl;// }
作者: mougaidong 发布时间: 2011-12-18
作者: jronald 发布时间: 2011-12-18
oss还没析构啊,那指针怎么就无效了?
因为,指针不是指向oss的数据成员,而是一个函数返回的临时对象的数据成员,它析构了。看看楼上的例子就明白了
作者: mougaidong 发布时间: 2011-12-18
作者: qq120848369 发布时间: 2011-12-18
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28