字符串连接在C中

时间:2016-03-06 13:31:04

标签: c string

void xsdValidation(char *xsdName, char *xmlName){
 char *terminalCommand;
 system("xmllint --noout --schema ", xsdName , xmlName);
}

我有点问题。我有一个代码来保护我的xml。我的xml名称和xsd的名字来自于一个参数。我怎么能连续这三件事?

3 个答案:

答案 0 :(得分:3)

使用snprintf()

/* determine buffer size */
int len = snprintf(NULL, 0, "xmllint, --noout --schema %s %s", xsdName, xmlName);
if (len < 0) {
    /* error handling for EILSEQ here */
}

char *buf = malloc(len + 1);
if (buf == NULL) {
    /* err handling for malloc() failure here */
}

snprintf(buf, (size_t)len, "xmllint, --noout --schema %s %s", xsdName, xmlName);

system(buf);
free(buf);

在一个足够新的系统上,您还可以使用asprintf()来大大简化此代码:

char *buf = NULL;
asprintf(&buf, "xmllint, --noout --schema %s %s", xsdName, xmlName);
if (buf == NULL) {
    /* error handling here */
}

system(buf);
free(buf);

请注意,如果xsdNamexmlName包含空格或其他特殊字符,则所有这些方法都会失败。您可能希望直接调用exec函数来避免该问题。

答案 1 :(得分:2)

您可以使用strcat()来连接字符串。

void xsdValidation(char *xsdName, char *xmlName){
 static const char *xmlLint = "xmllint --noout --schema ";
 /* do not forget to +1 for terminating null character */
 char *terminalCommand = malloc(strlen(xmlLint) + strlen(xsdName) + strlen(xmlName) + 1);
 if(terminalCommand != NULL){
  strcpy(terminalCommand, xmlLint); /* use strcpy() here to initialize the result buffer */
  strcat(terminalCommand, xsdName);
  strcat(terminalCommand, xmlName);
  system(terminalCommand);
  free(terminalCommand);
 }
}

这可能会稍微改善性能,因为它会重复使用计算的长度。

void xsdValidation(char *xsdName, char *xmlName){
 static const char *xmlLint = "xmllint --noout --schema ";
 size_t xmlLintLen = strlen(xmlLint);
 size_t xsdNameLen = strlen(xsdName);
 size_t xmlNameLen = strlen(xmlName);
 /* do not forget to +1 for terminating null character */
 char *terminalCommand = malloc(xmlLintLen + xsdNameLen + xmlNameLen + 1);
 if(terminalCommand != NULL){
  /* use strcpy() to copy the strings */
  strcpy(terminalCommand, xmlLint);
  strcpy(terminalCommand + xmlLintLen, xsdName);
  strcpy(terminalCommand + xmlLintLen + xsdNameLen, xmlName);
  system(terminalCommand);
  free(terminalCommand);
 }
}

答案 2 :(得分:0)

如果编译器支持可变长度数组,那么您可以编写

void xsdValidation( const char *xsdName, const char *xmlName )
{
    const char *command = "xmllint --noout --schema ";
    char terminalCommand[strlen( command ) + strlen( xsdName ) + strlen( xmlName ) + 2];

    strcpy( terminalCommand, command );
    strcat( terminalCommand, xsdName );
    strcat( terminalCommand, " " );
    strcat( terminalCommand, xmlName );

    system( terminalCommand );
}

否则,您应该动态分配所需的数组。例如

void xsdValidation( const char *xsdName, const char *xmlName )
{
    const char *command = "xmllint --noout --schema ";
    char *terminalCommand = 
        malloc( strlen( command ) + strlen( xsdName ) + strlen( xmlName ) + 2 );

    if ( terminalCommand != NULL )
    {    
        strcpy( terminalCommand, command );
        strcat( terminalCommand, xsdName );
        strcat( terminalCommand, " " );
        strcat( terminalCommand, xmlName );

        system( terminalCommand );

        free( terminalCommand );
    }
}