Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Aug 17, 1998

Function sscanf

Question:
Could you tell me what the proper syntax is for the sscanf function, and what return/error codes exist. This was for a Sparc-compiled program.

Answer:
Though newer programmers seem to favor the stream I/O classes, both sprintf and sscanf are still available and are quite powerful.

There's no way I can begin to cover all the options these two functions support, but they use similar format codes. sprintf formats one or more values to a string, and sscanf extracts one or more values from a formated string.

char lpstr[45] = "StringOne StringTwo 3 4";
char lpstr1[20], lpstr2[20];
int i1, i2;

sscanf(lpstr, "%s %s %d %d", lpstr1, lpstr2, &i1, &i2);
This example shows how you can extract four fields from a string with a single call to sscanf. In addition, sscanf returns the number of fields successfully converted and so you can use the return value to flag errors in the input string.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date