FormatSpecificationFields:scanfandwscanfFunctions
Aformatspecificationhasthefollowingform:
%[width][{h|l|I64|L}]type
Theformatargumentspecifiestheinterpretationoftheinputandcancontainoneormoreofthefollowing:
White-spacecharacters:blank('');tab('\t');ornewline('\n').Awhite-spacecharactercausesscanftoread,butnotstore,allconsecutivewhite-spacecharactersintheinputuptothenextnon–white-spacecharacter.Onewhite-spacecharacterintheformatmatchesanynumber(including0)andcombinationofwhite-spacecharactersintheinput.
Non–white-spacecharacters,exceptforthepercentsign(%).Anon–white-spacecharactercausesscanftoread,butnotstore,amatchingnon–white-spacecharacter.Ifthenextcharacterinstdindoesnotmatch,scanfterminates.
Formatspecifications,introducedbythepercentsign(%).Aformatspecificationcausesscanftoreadandconvertcharactersintheinputintovaluesofaspecifiedtype.Thevalueisassignedtoanargumentintheargumentlist.
Theformatisreadfromlefttoright.Charactersoutsideformatspecificationsareexpectedtomatchthesequenceofcharactersinstdin;thematchingcharactersinstdinarescannedbutnotstored.Ifacharacterinstdinconflictswiththeformatspecification,scanfterminates,andthecharacterisleftinstdinasifithadnotbeenread.
Whenthefirstformatspecificationisencountered,thevalueofthefirstinputfieldisconvertedaccordingtothisspecificationandstoredinthelocationthatisspecifiedbythefirstargument.Thesecondformatspecificationcausesthesecondinputfieldtobeconvertedandstoredinthesecondargument,andsoonthroughtheendoftheformatstring.
Aninputfieldisdefinedasallcharactersuptothefirstwhite-spacecharacter(space,tab,ornewline),oruptothefirstcharacterthatcannotbeconvertedaccordingtotheformatspecification,oruntilthefieldwidth(ifspecified)isreached.Iftherearetoomanyargumentsforthegivenspecifications,theextraargumentsareevaluatedbutignored.Theresultsareunpredictableiftherearenotenoughargumentsfortheformatspecification.
Eachfieldoftheformatspecificationisasinglecharacteroranumbersignifyingaparticularformatoption.Thetypecharacter,whichappearsafterthelastoptionalformatfield,determineswhethertheinputfieldisinterpretedasacharacter,astring,oranumber.
Thesimplestformatspecificationcontainsonlythepercentsignandatypecharacter(forexample,%s).Ifapercentsign(%)isfollowedbyacharacterthathasnomeaningasaformat-controlcharacter,thatcharacterandthefollowingcharacters(uptothenextpercentsign)aretreatedasanordinarysequenceofcharacters,thatis,asequenceofcharactersthatmustmatchtheinput.Forexample,tospecifythatapercent-signcharacteristobeinput,use%%.
Anasterisk(*)followingthepercentsignsuppressesassignmentofthenextinputfield,whichisinterpretedasafieldofthespecifiedtype.Thefieldisscannedbutnotstored.
|