12-5-21关于sscanf

头文件 #include


            定义函数 int sscanf (const char *str,const char * format,........);


            函数说明 
            sscanf()会将参数str的字符串根据参数format字符串来转换并格式化数据。格式转换形式请参考scanf()。转换后的结果存于对应的参数内。




            返回值 成功则返回参数数目,失败则返回-1,错误原因存于errno中。 返回0表示失败    否则,表示正确格式化数据的个数    例如:sscanf(str,"%d%d%s", &i,&i2, &s);    如果三个变成都读入成功会返回3。    如果只读入了第一个整数到i则会返回1。证明无法从str读入第二个整数。  


            范例 #include 
            main() 
            { 
            int i; 
            unsigned int j; 
            char input[ ]=”10 0x1b aaaaaaaa bbbbbbbb”; 
            char s[5]; 
            sscanf(input,”%d %x %5[a-z] %*s %f”,&i,&j,s,s); 
            printf(“%d %d %s ”,i,j,s); 
            }


            执行 10 27 aaaaa




            sscanf(stringBuf.c_str(), "%20[^#]#%20[^ ]",......)语句中""中的内容含义为:


            “%[ 
            ]”符号用于声明字符串,它比“%s”更具体,可以用于设置读取的样式。例如“%[a-z]”只读取小写字母,读到其它字符就结束。注意,方括号中如果有“^”,代表一直读到某字符为止。例如:




            “%[^#]”:读取字符串,一直到出现“#”号为止。


            “%20[^#]”:读取20个字节的字符串,出现“#”号时结束。


            所以,“%20[^#]#%20[^ ]”的意义就是,


            读取两个20字节大小的字符串,第一个字符串可以用#结束,第二个字符串可以用回车符结束。


            它们的具体阐述,参见MSDN:“scanf Type Field Characters”章节,和“scanf Width 
            Specification”章节。


*********************************************************************************************************************************************


大家都知道sscanf是一个很好用的函数,利用它可以从字符串中取出整数、浮点数和字符串等等。它的使用方法简单,特别对于整数和浮点数来说。但新手可能并不知道处理字符串时的一些高级用法,这里做个简要说明吧。


  1. 常见用法。


以下是引用片段:
  charstr[512]={0};
  sscanf("123456","%s",str);
  printf("str=%s
",str);


  2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。


以下是引用片段:
  sscanf("123456","%4s",str);
  printf("str=%s
",str);


  3. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。


以下是引用片段:
  sscanf("123456abcdedf","%[^]",str);
  printf("str=%s
",str);


  4. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。


以下是引用片段:
  sscanf("123456abcdedfBCDEF","%[1-9a-z]",str);
  printf("str=%s
",str);


  5. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。


以下是引用片段:
  sscanf("123456abcdedfBCDEF","%[^A-Z]",str);
  printf("str=%s",str);


*********************************************************************************************************************************************


名称: sscanf() - 从一个字符串中读进与指定格式相符的数据. 


语法: int sscanf( string str, string fmt, mixed var1, mixed var2 ... );


整数 sscanf( 字符串 str, 字符串 fmt, 混合 var1, 混合 var2 ... );


用法: 以指定的格式 fmt 去解读字符串 str. fmt 中除了 %d 和 %s 以外, 亦可包含其他的字符串作为格式. 每一个 %d 或 %s 都对应一个参数, 按顺序为 var1, var2 ... %d 读入一个整数到参数中, 而 %s 读入一个字符串. * 亦可用于格式中, (即 %*d 和 %*s) 加了星号 (*) 表示跳过此数据不读入. (也就是不把此数据读入参数中) LPC 的 sscanf() 与 C 的 sscanf() 虽然相似, 但仍有不同之处. LPC 的 sscanf() 不需要 (也不可) 提供变量的内存位址给 sscanf(), 只需要给予变量的名字. 另一个不同点是, LPC 的 sscanf() 对于: sscanf( str, "%s %s", str1, str2 ); 的语法, 将会把 str 中的第一个英文单字 (即第一个空白字符以前的内容) 读入 str1, 后面其余的内容读入 str2.


sscanf() 会返回符合格式的 %d 和 %s 总数.


以前曾经编写过这样的小程序:一个文本文件,每行是一条记录,每条记录中包含多个字段,每个字段之间以某种定界符分开,举例如下:


Notebook,IBM,ThinkPad X32,6,12000


(各字段以逗号分隔,内容依次是:物品名称,生产厂家,型号,数量,价格)


如果要对这样的一行记录进行处理,提取出各个字段,怎么做比较好呢?


我以前的做法是在一个循环中用strtok函数每次取一个字段,然后将内容保存到一个字符串数组中。这样做虽然可行,但我总感觉写出的代码有些啰嗦。


最近看到一段代码,用C的标准库函数sscanf,处理这样的数据,只需一行就可以了。我把代码整理了一下,去掉了无关的部分,核心部分如下:


float price;


int quantity;


char category[21], name[21];


char vendor[21], sku[21];


char buf[201];


fp = fopen(filename, "r");


fgets(buf, 200, fp);


sscanf(buf,


"%20[^#]#%20[^#]#%f#%i#%20[^#]#%20[^/n]",


name, sku, &price, &quantity, category, vendor);


下面简单做些解说:


%20[^#]# 最多读入20个字符,直到遇见定界符#,但不包含定界符


%f# 读入一个浮点数,直到遇见定界符#


%i# 读入一个整数,直到遇见定界符#


%20[^/n] 最多读入20个字符,忽略行尾的回车符


是不是很简洁明了呢?


#include <stdio.h>


int main()


{


char log[]="<14>2002-11-11 12:12:12 11.22.33.44 3 3 aaaa aaaaaa";


//char log[]="<1>2002-11-11 12:12:12 11.22.33.44 3 aaaa aaaaaa";


char test[]="<1111> 22";


char log2[200];


char str1[20];


char str2[20];


char str3[20];


char str4[20];


char str5[20];


char str6[20];


char str7[20];


int a1,a2,a3,a4,a5,a6;


sscanf(log,"<%d>%s %s %s %d %d %s",&a1,str2,str3,str4,&a5,&a6,str7);


printf("%d/n",a1);


printf("%s/n",str2);


printf("%s/n",str3);


printf("%s/n",str4);


printf("%d/n",a5);


printf("%d/n",a6);


printf("%s/n",str7);


sscanf(test,"<%d> %d",&a5,&a6);


printf("%d/n",a5);


printf("%d/n",a6);


sscanf(log,"<%[^>]>%[^ ] %[^ ] %[^ ] %[^ ] %[^ ] %[^$]",str1,str2,str3,str4,str5,str6,str7);


printf("%s/n",str1);


printf("%s/n",str2);


printf("%s/n",str3);


printf("%s/n",str4);


printf("%s/n",str5);


printf("%s/n",str6);


printf("%s/n",str7);


return 1;


}


const char *str = "drw-rw-rw- 1 user group 0 Oct 28 2003 -====流行音乐专用FTP=====-";


上面是源串,我要分别得到drw-rw-rw-,group,-====流行音乐专用FTP=====-字段


注意:因为这几个字段的值会变化,所以我要用格式化输入,分别存入下面的a b c中,高手帮忙!


下面这个是我没成功的尝试


char a[20];


char b[50];


char c[20];


int ret = sscanf(str, "%[^'' '']* %[''u''] %[^'' '']"


, a, b, c);


masterz(www.fruitfruit.com) 于 2005-8-6 17:43:49


sscanf可以支持regular expression?或许你要用boost,下面有一个简单的例子,虽然离你的问题还比较远


http://www.fruitfruit.com/vc/boost/boost_regex_test.cpp


newbiestar 于 2005-8-6 18:29:18


楼主一个问题好几个地方问……


int ret = sscanf(str, "%s%*s%*s%s%*s%*s%*s%*s%s", a, b, c);


这样就可以了,不要的东西都抛弃掉了


参见这个帖子里面我的回复


http://community.youkuaiyun.com/Expert/topic/4192/4192979.xml?temp=.2922632


ding8125(丁丁) 于 2005-8-6 19:19:12


可以通过字符数组输出一个字符串!~


今天看到一个奇怪的scanf。其实这只是用了正则表达式。


sscanf(user, "%127[^:]:%127[^ ]", user_name, password);


"%127[^:]:%127[^ ]",是正则表达式


用scanf或者printf,可以在%后面跟%d,%s等东西,也可以跟一个正则表达式。


这里,127表示最多可以接受127个字符,[^:]是正则表达式,表示非":",到":"结束


后面,%127[^ ],同样,其中[^ ]是正则表达式,表示非" ",到" "结束


所以,如果user是"wpc:123456"的字符串,那么经过上面的sscanf后,


user_name是wpc,而password是123456
function [ctd,varlabel,names,sensors]=ctd_rd(cnv_file,FMT) %#ok<*STOUT> % CTD_RD Reads the SeaBird ASCII .CNV file format % % Usage: CTD=ctd_rd(cnv_file); % % Input: cnv_file = name of .CNV file (e.g. 'cast002.cnv') % % Output: CTD - a data structure containing the CTD data and header info. % % There are various formats possible for time/location information. % Some of these can be handled with an optional second input parameter: % CTD=ctd_rd(cnv_file,FORMAT) % % where FORMAT = 'NMEA' for SEASAVE output with GPS included through deckbox % % * System UpLoad Time = Oct 10 1999 18:20:32 % * NMEA Latitude = 48 36.69 N % * NMEA Longitude = 123 13.07 W % * NMEA UTC (Time) = Oct 10 1999 18:20:32 % % 'IR' for an internally recording mode with a hand-typed % header in this form: % % ** Latitude = 49 21.092 N % ** Longitude = 121 49.028 W % ** PST (Time) =Nov 23 2000 07:45 % % will also work with SEASAVE output if no % GPS NMEA string available. % (default mode) % % If no '** Time' line gets it from System Upload Time % % 'RP' Ignore any header data but decode System Upload Time for % the year, and the 'cast' line (DH command) for time - % good for IR mode when to-yo-ing or otherwise too lazy % to create proper headers. % % 'LAB' for a mode with no NMEA or header data (i.e. lab % testing) % % Add more lat,lon and date string handling if your .CNV files are different. % % 4-8-98 Rich Signell (rsignell@usgs.gov) % incorporates ideas from code by Derek Fong & Peter Brickley % 19-9-99 R. pawlowicz (ric@ocgy.ubc.ca) % - added a lot more decoding plus the idea of a data stucture for output % 12-6-00 Changes variable names slightly. % 19-2-02 dms as well as dmm.mm formats % 20-5-11 modified by Rowan Fox (ro1@uvic.ca), adding in model #, temp % sensor SN # & cond sensor SN % 16-5-12 can now read new-format cnv files with xml info if nargin==1 FMT='IR'; end ctd.name='ctd'; ctd.station=''; % Open the .cnv file as read-only text if contains(lower(cnv_file),'.cnv') cnv_file=[cnv_file '.cnv']; end fid=fopen(cnv_file,'r'); % Read the header. lat=[]; lon=[]; gtime=NaN(1,6); tzone='unknown'; % Stop at line that starts with '*END*' str='*START*'; while (~strncmp(str,'*END*',5)) str=fgetl(fid); switch FMT case 'NMEA' if (strncmp(str,'* NMEA Lat',10)) lat=get_lat(str); elseif (strncmp(str,'* NMEA Lon',10)) lon=get_lon(str); elseif (strncmp(str,'* NMEA UTC',10)) gtime=get_timestamp(str); tzone='UTC'; end case 'IR' if any(strfind(str,'Latitude')) || any(strfind(str,'Lat')) lat=get_lat(str); elseif any(strfind(str,'Longitude')) || any(strfind(str,'Long')) lon=get_lon(str); % Read the TIME string. This may vary with CTD setup. Will need % ** xxxxxxxxxxTimexxxx = 21 Dec 2005 hh:mm:ss % where timezone may also be in there somewhere. elseif strncmp(str,'**',2) && any(strfind(str,'Time')) gtime=get_timestamp(str); tzone='unknown'; if any(strfind(str,'PST')) tzone='PST'; elseif any(strfind(str,'PDT')) tzone='PDT'; elseif any(strfind(str,'UTC')) tzone='UTC'; elseif any(strfind(str,'EST')) tzone='EST'; elseif any(strfind(str,'EDT')) tzone='EDT'; end % The default - get a time from the 'upload' string (usually the other time % strings are later in the file so they will overwrite this, if they exist). elseif (strncmp(str,'* System UpLoad',15)) tzone='unknown'; gtime=get_timestamp(str); end case 'LAB' if (strncmp(str,'* System UpLoad',15)) tzone='unknown'; gtime=get_timestamp(str); end case 'RP' if (strncmp(str,'* System UpLoad',15)) is=strfind(str,'='); %EXTRACT THE UPLOAD YEAR FOR CAST START TIME Roger 2003sep04 upyearstr=str(is+9:is+12); end %------------------------------------------------------------------- % READ CAST START TIME, SAMPLE RATE, COMPUTE mtime Roger 2003sep04 % * cast 0 06/23 17:38:06 samples 0 to 1334 sample rate = 1 scan every 0.5 seconds if (strncmp(str,'* cast ',7)) %Find slash indslash=find(str=='/'); indcolon=find(str==':'); if length(indslash)==1 && length(indcolon)>=2 datstr=[upyearstr ' '... str(indslash-2:indslash-1) ' ' ... str(indslash+1:indslash+2) ' ' ... str(indcolon(1)-2:indcolon(1)-1) ' '... str(indcolon(1)+1:indcolon(2)-1) ' '... str(indcolon(2)+1:indcolon(2)+2)]; mtime=datenum(str2double(datstr)); gtime=datevec(mtime); fprintf('Date from CAST START TIME in timezone of SBE clock\n'); else error('ERROR: CAST START TIME: str does not have expected form') end end % otherwise error('Unrecognized format specifier'); end %----------------------------- % % Read the station name from a comment line % if (strncmpi((str),'** station',10)) ctd.station=strtrim(str(min(strfind(str,':'))+2:end)); %----------------------------- % % Read the ctd model from a comment line % elseif (strncmp(str,'* Sea-Bird',10)) ctd.model=str(strfind(str,'Sea-Bird SBE ')+13:strfind(str,' Data File')-1); %----------------------------- % % Read the ctd temperature sensor SN from a comment line % elseif (strncmp(str,'* Temperature SN = ',19)) ctd.tempsn=str(strfind(str,'* Temperature SN = ')+19:end); %----------------------------- % % Read the ctd conductivity sensor SN from a comment line % elseif (strncmp(str,'* Conductivity SN = ',20)) ctd.condsn=str(strfind(str,'* Conductivity SN = ')+20:end); %----------------------------- % % Read the depth from a comment line % elseif (strncmpi((str),'** depth',8)) idelim=strfind(str,':'); ctd.depth=sscanf(str(idelim+1:end),'%f'); if isempty(ctd.depth), ctd.depth=NaN; end % Units? strrem=str(idelim+1:end);strrem(strrem>=48 & strrem<=57)=' '; %#ok<*NASGU> if any(strfind(str(idelim+1:end),' m')) % OK elseif any(strfind(str(idelim+1:end),' ft')) ctd.depth=ctd.depth*0.3048; end %------------------------------ % % Get sampling interval % elseif (strncmp(str,'# interval',10)) ctd.samp_interval=sscanf(str(strfind(str,':')+1:end),'%f'); %------------------------------ % % Get start time % elseif (strncmp(str,'# start_time',12)) ctd.start_time=str(15:end); ctd.start_mtime=datenum(str(15:end),'mmm dd yyyy HH:MM:SS'); %------------------------------ % % Read the variable names & units into a cell array % elseif (strncmp(str,'# name',6)) var=sscanf(str(7:10),'%d',1); var=var+1; ctd.varlabel{var}=str(strfind(str,'=')+2:min([strfind(str,':')-1 ... strfind(str,'/')-1 strfind(str,'-')-1])); %------------------------------ % % Read the sensor names into a cell array % elseif (strncmp(str,'# sensor',8)) sens=sscanf(str(10:11),'%d',1); sensors{sens+1}=str; %#ok<*AGROW> %------------------------------ % % Read the sensor ranges into a cell array % elseif (strncmp(str,'# span',6)) sens=sscanf(str(8:9),'%d',1); ctd.spans{sens+1}=sscanf(str(strfind(str,'=')+1:end),'%f,%f'); % % pick up bad flag value elseif (strncmp(str,'# bad_flag',10)) isub=13:length(str); bad_flag=sscanf(str(isub),'%g',1); end end %============================================== % Done reading header. Now read the data! nvars=var; %number of variables % Read the data into one big matrix data=fscanf(fid,'%f',[nvars inf]).'; fclose(fid); % Flag bad values with NaN data(data==bad_flag)=NaN; if exist('sensors'), ctd.sensors=char(sensors); end %#ok<*EXIST> ctd.latitude=lat; ctd.longitude=lon; ctd.gtime=gtime; ctd.mtime=datenum(gtime(1),gtime(2),gtime(3),gtime(4),gtime(5),gtime(6)); ctd.tzone=tzone; if strcmp(FMT,'RP') ctd.mtimescan=(0:size(data,1)-1)*ctd.samp_interval/86400+mtime; end labeldel=[]; for k=1:length(ctd.varlabel) if diff(ctd.spans{k})~=0 eval(['ctd.' ctd.varlabel{k} '=data(:,k);']); else labeldel=[labeldel k]; end end ctd.varlabel(labeldel)=[]; ctd.spans(labeldel)=[]; return %--------------------------------------------------- function lon=get_lon(str) % decodes latitude from input string % Get delimiter - '=' or ':' is=strfind(str,'='); if isempty(is), is=strfind(str,':'); end isub=is+1:length(str); dm=sscanf(str(isub),'%f'); % E or W if(contains(str(isub),'E')) sigl=1; else sigl=-1; end % dd.ddd or dd mm.mmm or dd mm ss formats switch (length(dm)) case 1 lon=sigl*dm; case 2 lon=sigl*(dm(1)+dm(2)/60); case 3 lon=sigl*(dm(1)+dm(2)/60+dm(3)/3600); otherwise lon=NaN; disp(['Can''t scan string ->' str '<- for longitude']); end return; %--------------------------------------------------- function lat=get_lat(str) % decodes latitude from input string % Get delimiter - '=' or ':' is=strfind(str,'='); if isempty(is), is=strfind(str,':'); end isub=is+1:length(str); dm=sscanf(str(isub),'%f'); % N or S if(contains(str(isub),'N')) sigl=1; else sigl=-1; end % dd.ddd or dd mm.mmm or dd mm ss formats switch (length(dm)) case 1 lat=sigl*dm; case 2 lat=sigl*(dm(1)+dm(2)/60); case 3 lat=sigl*(dm(1)+dm(2)/60+dm(3)/3600); otherwise lat=NaN; disp(['Can''t scan string ->' str '<- for longitude']); end return; %-------------------------------------------------- function gtime=get_timestamp(str) % Decodes time string % Time to right of '=' idelim=strfind(str,'='); if any(idelim) str=str(idelim+1:end); else % try a colon delimiter idelim=strfind(str,':'); if any(idelim) str=str(idelim(1)+1:end); end end % is there hh:mm:ss in there? is=strfind(str,':'); if any(is) isub=is(1)-2:length(str); % Date in Dec 21 2005 HH:MM format nbl=find(str(1:is(1)-3)~=' '); datstr=str(min(nbl):max(nbl)); ibl=strfind(datstr,' '); if length(ibl)==2 % Write into 21-Dec-2001 format which matlab can decode datstr=[datstr(ibl(1)+1:ibl(2)-1) '-' datstr(1:ibl(1)-1) '-' datstr(ibl(2)+1:end)]; if ibl(2)-ibl(1)==2 datstr=['0' datstr]; end gtime=datevec(datstr); % Add hh:mm:ss if length(is)==1 gtime(4:5)=sscanf(str(isub),'%d:%2d'); elseif length(is)==2 gtime(4:6)=sscanf(str(isub),'%d:%2d:%2d'); else disp(['Can''t scan string ->' str '<- for time of day']); gtime=[0 0 0 0 0 0]; end else disp(['Can''t scan string ->' str '<- for day of year']); gtime=[0 0 0 0 0 0]; end else disp(['Can''t scan string ->' str '<- for time of day']); gtime=[0 0 0 0 0 0]; end
最新发布
11-04
[2025-07-16 09:20:54.167] RX:4 Rect: (220,5) (221,11) (41,214) (39,189) Received 16 bytes Received 16 bytes [2025-07-16 09:21:06.392] RX:Manual NEXT signal sent Sent NEXT signal to K210 Received 16 bytes [2025-07-16 09:21:12.094] RX:1 2 Manual NEXT signal sent Sent NEXT signal to K210 Start following rectangle Rect: (-1,-1) (-1,-1) (305,152) (302,217) Received 16 bytes Moving to corner 0 Converted: (-1,-1) -> (-15.09,10.08) Received 16 bytes [2025-07-16 09:21:22.405] RX:Manual NEXT signal sent Sent NEXT signal to K210 Received 16 bytes Received 16 bytes [2025-07-16 09:21:31.843] RX:Manual NEXT signal sent Sent NEXT signal to K210 Received 16 bytes Manual NEXT signal sent Sent NEXT signal to K210 1 2 Manual NEXT signal sent Sent NEXT signal to K210 Stop following rectangle Received 16 bytes At origin [2025-07-16 09:21:39.654] RX:1 2 Start following rectangle [2025-07-16 09:21:42.433] RX:Manual NEXT signal sent Sent NEXT signal to K210 Received 16 bytes [2025-07-16 09:21:47.690] RX:Manual NEXT signal sent Sent NEXT signal to K210 Received 16 bytes [2025-07-16 09:21:52.091] RX:Manual NEXT signal sent Sent NEXT signal to K210 Received 16 bytes [2025-07-16 09:21:57.142] RX:Manual NEXT signal sent Sent NEXT signal to K210 Received 16 bytes Manual NEXT signal sent Sent NEXT signal to K210 Rect: (36,48) (274,40) (271,206) (46,195) Received 16 bytes Sent NEXT signal to K210 Received 16 bytes [2025-07-16 09:22:04.576] RX:Manual NEXT signal sent Sent NEXT signal to K210 Received 16 bytes [2025-07-16 09:22:09.093] RX:Manual NEXT signal sent Sent NEXT signal to K210 Received 16 bytes
07-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值