Use of sscanf () and sprintf () in C language

Source: Internet
Author: User

#include <stdio.h>void main (void) {    char * cgistr = ' | ip=192.168.1.78| port=5678| dbname=haha| user=hehe| Pwd=123456| ";    Char cbusip[16]= "";    int nport = 0;    Char cdbname[64] = "";    Char cuser[64] = "";    Char cpwd[64] = "";    Char cquestcmd[16]= "";    SSCANF (Cgistr, "| ip=%[^|]| port=%d| dbname=%[^|]| user=%[^|]| Pwd=%[^|]| ", Cbusip, &nport, Cdbname, Cuser, CPWD);    printf (CBUSIP);    printf ("\ n");    sprintf (Cquestcmd, "Hget%s_infor OWner", cdbname);    printf (cquestcmd);    printf ("\ n");}

Console output:

192.168.1.78
Hget haha_infor OWner
It's a sight to see.

sscanf (parameter 1, parameter 2, Parameter 3 ...) is to assign the desired field in Parameter 1 to parameter 3 by the form of parameter 2 ....

sprintf (parameter 1, parameter 2, Parameter 3 ...) is the parameter 3 ... The field is stitched to parameter 1 in the form of parameter 2.

"%n[a-z]" reads up to n characters and stops if a non-a-Z character is encountered
"%[^=]" reads any number of characters until it encounters "=" Stop
"%n[^=" reads up to n characters before the "=" sign

Use of sscanf () and sprintf () in C language

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.