1.sscanf用于處理固定格式的字符串,包含在頭文件<cstdio>中,函數原型為:
? ? ? ? ? ? ? ?int sscanf(const char *buffer,const char*format,[]argument? ]...);
其中:buffer代表著要存儲的數據,format 代表格式控制字符串,argument選擇性設定字符串(分割之后存儲數據的地址);
返回值:成功轉換的值的個數,失敗則返回-1;
函數功能:sscanf會從buffer里讀取數據,依照format的格式將數據寫入argument中;
2.從string字符串中獲得const char*,使用string.data()或者string.c_str()(最好使用這個);
二者的區別在于:string.data()只是返回原始數據序列,并不把‘\0’來作為字符串結束標志;
string.c_str()是標準的做法:返回的const char*是一個合法的用‘\0’終止的C兼容字符串。
其實c_str()包含了data(),具體實現如下:
const char*c_str()const{
? ? ? ? ? ? ? ? ?if(length()==0)
? ? ? ? ? ? ? ? ? ? ? ? return "";
? ? ? ? ? ? ? ? ?terminate();
? ? ? ? ? ? ? ?return data();
}
另外一個重要的是:只在需要時才使用c_str()或者data()得到字符串,每調用一次,下次再調用就失效了。
?
?
?