Sprintf/sscanf usage

Source: Internet
Author: User

1,

Sprintf has almost the same usage as printf, but the printing destination is different. The former is printed to the string, and the latter is directly output on the command line. This also makes sprintf much more useful than printf.
Sprintf is a variable parameter function, which is defined as follows:
Int sprintf (char * buffer, const char * Format [, argument]...);
In addition to the fixed types of the first two parameters, you can take over multiple parameters later. Its essence is obviously on the second parameter: Format String.

Format a numeric string
One of the most common applications of sprintf is to print Integers to strings. Therefore, spritnf can replace ITOA in most cases.
For example, print the integer 123 into a string and save it in S.
Sprintf (S, "% d", 123); // generate "123"
You can specify the width. If the width is insufficient, spaces are filled on the left:
Sprintf (S, "% 8d % 8d", 123,456 7); // generate: "123 4567"
Of course, you can also align left:
Sprintf (S, "%-8d % 8d", 123,456 7); // generate: "123 4567"


Sprintf Return Value
Few people pay attention to the return values of the printf/sprintf function, but sometimes it is useful. spritnf returns the function call.
The number of characters that are finally printed to the character buffer. That is to say, after a sprinf call ends, you do not need to call it again.
Strlen knows the length of the result string. For example:
Int Len = sprintf (S, "% d", I );
For a positive integer, Len is equal to the 10-digit digits of the integer I.


Eg.Len = sprintf (page, "% s = '% s'/N", fb_data-> name, fb_data-> value ); 

2,


Sscanf () function usage int sscanf (const char * buffer, const char * Format [, argument]...)

In fact, the sscanf function is similar to the scanf function, except that scanf is input by the user on the console, while sscanf reads data from the buffer, instead of user input (equivalent to the user input string is buffer ). the argument is the location to store. During the matching process, sscanf stops running as long as there is a mismatch.

The Return Value of the sscanf function indicates how many variables are assigned a value.



# Include <iostream> <br/> # include <string> <br/> using namespace STD; <br/> const int maxn = 3000; <br/> int f [maxn]; <br/> int main () {<br/> char * P = "1234 "; <br/> char s [10]; <br/> int A, B; <br/> sscanf (P, "% s", S ); <br/> printf ("% s/n", S); <br/> sscanf (P, "% 3d % 1D", & A, & B ); <br/> printf ("% d/N", a, B); <br/> return 0; <br/>}< br/> 



 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.