將一個只有數字的字符串轉換成實數,可以用atof(buf);但是如果字符串裏包含其他非數字字符,就需要用到sscanf().
例如從一個文件名字符串中提取時間:
#include <stdio.h>
#include <stdlib.h>
void main()
{
char buf[50]="VOL_20151001162015.h264";
double videoTime;
sscanf_s(buf,"VOL_%lf.h264",&videoTime);
printf("%.0lf\n",videoTime);
system("pause");
}
sscanf和scanf用法相似,只不過sscanf是固定字符串作爲輸入源,scanf是鍵盤爲輸入源。
這個函數的原型在stdio.h中。具體用法可參考:
http://blog.chinaunix.net/uid-26284412-id-3189214.html
http://www.91linux.com/html/article/program/cpp/20081130/14121.html
同樣sprintf()是輸出到字符串中,用法和print()相似。具體用法可參考:
http://www.cnblogs.com/Anker/p/3351168.html