一个非常操蛋的问题

我一直想的是利用C++string类封装的函数的便捷性和linux下的C/API结合来
实现某些方法。可是有个问题出现了,每次碰见string和char*之间的转换的时候极为的尴尬。

  #include <iostream>
  #include <stdio.h>
  #include <unistd.h>
  #include <iostream>
  #include <string>
  #include <sys/types.h>          /* See NOTES */
  #include <sys/socket.h>
  #include <arpa/inet.h>
  #include <sys/stat.h>
  #include <fcntl.h>
  using namespace std;


int main(int argc, char const *argv[])
{
 int fd = open("./ok",O_RDWR);

// char buf[1024];
string buf = new char[1023];
read(fd,const_cast<char*>(buf.c_str()),1024);
cout << buf << endl;
// string buf2 = (string)buf;
cout << buf.find_first_of(" ",0);
cout << "*********" << endl;

return 0;
}

read的第二个参数原本是void,一般使用的时候是用char来做的,把独到的信息存储在buf里面,为了以后方便,我希望在这里直接用一个string来存储信息。
我清楚string和char*并不是兼容的,所以我通过const_cast来转换。
可是尴尬的是并没有读到什么信息。
这是为什么呢?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容