我一直想的是利用C++string類封裝的函數(shù)的便捷性和linux下的C/API結(jié)合來
實(shí)現(xiàn)某些方法雕憔《崴可是有個問題出現(xiàn)了冲呢,每次碰見string和char*之間的轉(zhuǎn)換的時候極為的尷尬肴盏。
#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的第二個參數(shù)原本是void威沫,一般使用的時候是用char來做的,把獨(dú)到的信息存儲在buf里面伤哺,為了以后方便燕侠,我希望在這里直接用一個string來存儲信息。
我清楚string和char*并不是兼容的立莉,所以我通過const_cast來轉(zhuǎn)換绢彤。
可是尷尬的是并沒有讀到什么信息。
這是為什么呢蜓耻?