Substitution of sprintf_s functions under Linux (reproduced)

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/yeahgis/archive/2013/01/22/2872179.html

Windows platform thread safe format string function sprint_s is not a standard C function, so Linux cannot be used, but you can use the snprintf function instead.

/*function Prototypes:*/intsnprintfChar*dest, size_t N,Const Char*fmt, ...);/*function Description: Copy n-1 characters from the source string to the target string, and then add a 0 to the back. So if the size of the target string is n, it will not overflow. function return value: Returns the number of characters stored in the array if successful, and a negative value if an error is encoded. Recommended usage:*/voidFConst Char*p) {    Charbuf[ One]={0}; snprintf (BUF,sizeof(BUF),"%10s", p);//Note: This 2nd parameter should be used with sizeof (STR) instead of hard-coded 11 and should not use sizeof (str)-1 orprintf"%SN", buf);}

Substitution of sprintf_s functions under Linux (reproduced)

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.