当前位置: 技术问答>linux和unix
C语言中的字符串拼接问题,怎么得不到我想要的字符串?
来源: 互联网 发布时间:2015-05-15
本文导语: char *mystr; char *tempstr; mystr = (char *)malloc(sizeof(char)*60); tempstr = (char *)malloc(sizeof(char)*60); mystr = "ABC"; sprintf(tempstr,"myfield like '%s%%'",mystr); //tempstr="myfield like 'ABC%'" 想达到tempstr = "myfield like '%ABC%'" 于是改成: sprin...
char *mystr;
char *tempstr;
mystr = (char *)malloc(sizeof(char)*60);
tempstr = (char *)malloc(sizeof(char)*60);
mystr = "ABC";
sprintf(tempstr,"myfield like '%s%%'",mystr);
//tempstr="myfield like 'ABC%'"
想达到tempstr = "myfield like '%ABC%'"
于是改成:
sprintf(tempstr,"myfield like '%%s%%'",mystr);
不行,串变成:
tempstr = "myfield like '%s%'"
请问怎么拼接?
char *tempstr;
mystr = (char *)malloc(sizeof(char)*60);
tempstr = (char *)malloc(sizeof(char)*60);
mystr = "ABC";
sprintf(tempstr,"myfield like '%s%%'",mystr);
//tempstr="myfield like 'ABC%'"
想达到tempstr = "myfield like '%ABC%'"
于是改成:
sprintf(tempstr,"myfield like '%%s%%'",mystr);
不行,串变成:
tempstr = "myfield like '%s%'"
请问怎么拼接?
|
sprintf(tempstr,"myfield like %c%s%c",'%',mystr,'%');