補充之前寫的:
一切都是從Hello World開始

原來scanf的format參數支援更複雜的格式描述!

參考資料:
https://cplusplus.com/reference/cstdio/scanf/
https://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf
https://stackoverflow.com/questions/25256697/scanf-doesnt-accept-whitespace

//int scanf ( const char * format, ... );

char buffer[100];
scanf("%99s", buffer);

以上常見寫法不支援空白' ',buffer只會存入空白前的字串,
譬如鍵入"Hello World",只有"Hello"會存入buffer

//換行符前的字元都會存入buffer
"%99[^\n]"

//數字、英文大小寫和空白字元存入buffer
"%99[0-9a-zA-Z ]"

這些條件語法很像正規表達式(Regular Expression/Regex),但不是完整支援

字串存入buffer後,還能搭配功能強大的sscanf做拆解剖析,已經有前輩寫很詳細的中文介紹和範例:
https://programmermagazine.github.io/201312/htm/article2.html
http://blog.gitdns.org/2017/03/08/sscanf/

以後遇到更多案例,再繼續補充介紹

創作者介紹
創作者 GNAySolution 的頭像
Yang

GNAySolution

Yang 發表在 痞客邦 留言(0) 人氣( 0 )