C 插入字符串的更好方法?

C 插入字符串的更好方法?,c,string,string-formatting,C,String,String Formatting,我想将一个变量字符串插入到一个预先确定的字符串中……以下是我到目前为止所做的简化版本(我省略了错误检查等): 有更好的方法吗 最好不要使用mallocrealloc,但我不想用固定长度实例化cmd变量 (最初我认为我可以这样做,然后是realloc..e.char cmd[50]=“CREATE…”或char*cmd=“CREATE…”,但是realloc只对以前使用过malloc的变量有效) 我能想到的唯一其他方法(我不确定这是否有效)是: void insertPath(char *path

我想将一个变量字符串插入到一个预先确定的字符串中……以下是我到目前为止所做的简化版本(我省略了错误检查等):

有更好的方法吗


最好不要使用
malloc
realloc
,但我不想用固定长度实例化
cmd
变量

(最初我认为我可以这样做,然后是realloc..e.
char cmd[50]=“CREATE…”
char*cmd=“CREATE…”
,但是
realloc
只对以前使用过
malloc
的变量有效)

我能想到的唯一其他方法(我不确定这是否有效)是:

void insertPath(char *path)
{
    char *cmd;
    cmd = (char *)malloc(55);
    strcpy(cmd, "CREATE VIRTUAL TABLE temp.tablename USING CSVFILE( %s);");

    int new_size = (strlen(cmd) + strlen(path));
    cmd = (char *)realloc(cmd, new_size);

    sprintf(cmd, path);

   // Do other stuff here and finally free(cmd);
}
编辑(回应评论):
sizeof(char)=>1
,+1用于终止
\0

snprintf怎么样

size_t size = baseSize + strlen(path) + 1;
char *cmd = malloc(size);
snprintf(cmd, size, "CREATE VIRTUAL TABLE temp.tablename USING CSVFILE(%s);", path);
这里,
baseSize
是“预先确定”字符串的长度,
path
是作为函数参数获得的变量字符串。最后,
cmd
应包含预先确定的文本,路径应替换为
%s

为什么不

void insertPath(char *path) {
    char const *tmp = "CREATE VIRTUAL TABLE temp.tablename USING CSVFILE(";
    char       *cmd = (char*) malloc(strlen(path) + strlen(tmp) + 3);
    strcpy(cmd, tmp);
    strcat(cmd, path);
    strcat(cmd, ");");
}

为什么不忘记
malloc
,从而避免代码中的内存泄漏

i、 e

然后使用

snprintf(cmd, len, "CREATE VIRTUAL TABLE temp.tablename USING CSVFILE(%s);", path);

忽略手动创建未初始化SQL语句的不明智做法,您可以使用
sprintf
函数,但要正确。参数是
输出
格式
参数
。例如:

char *insertPath(const char *path)
{
    static const char *cmd = "CREATE VIRTUAL TABLE temp.tablename USING CSVFILE(%s);";
    char *output = malloc(strlen(cmd) + strlen(path)); // Strictly this should be ... - 1 since it includes the the length of '%s'
    sprintf(output, cmd, path);
    return output;
}
尽量避免无需分配内存。在这里,模板字符串
cmd
从不被修改,因此可以声明为
static
const
。如果只需将两个字符串的长度相加,输出缓冲区就可以保证足够长。另外,请记住,如果需要在函数外部使用新字符串的值,则必须以某种方式返回指针。在您的示例中,您调用
realloc
,但从不让调用者知道内存块已更改。这通常会导致segfault,因为当扩展内存时,
realloc
可能会或可能不会更改内存的位置。

内存有什么问题

void insertPath(char*path)
{
const char cmd[]=“使用CSVFILE(%s)创建虚拟表temp.tablename;”;
int size=strlen(cmd)+strlen(path)+1;//*sizeof(char)如果它让你高兴的话
char*newcmd=malloc(大小);
snprintf(newcmd,size,cmd,path);
}

“最好不要使用malloc realloc”。封装它们。你能想到的唯一方法是什么?更一般地说,你的问题是什么?我的问题就在那里。。。“有没有更好的方法?”。我已经两次尝试将未知字符串插入到已知字符串中。我在问哪一个(或我不知道的第三个)是最好的方法Sizeof(char)=1 by standardis
PATH\u MAX
标准或我需要定义的东西?它在
limits.h
中#包括ltatalso请检查50是否足够,因为OP代表这不是一个要求。我想我会这样做,因为我不需要在函数返回后保留变量…谢谢。这几乎与我的答案一模一样,但更具屈尊感,没有解释。此外,你可以让
cmd
静态
@madpysicast,我们似乎是在同一时间发布的。真的。我想我们都这样做了:)顺便说一句,您不需要malloc中的+1,因为%s已经占用了两个额外的字符。最好将
tmp
声明为
const char*
char[]
。事实上,有些编译器会发出警告来告诉你这一点。@JohnBollinger。另外,最好还是将其设为静态。我已经隐式地得到了+2,因为
%s
在最终结果中消失了。
snprintf(cmd, len, "CREATE VIRTUAL TABLE temp.tablename USING CSVFILE(%s);", path);
char *insertPath(const char *path)
{
    static const char *cmd = "CREATE VIRTUAL TABLE temp.tablename USING CSVFILE(%s);";
    char *output = malloc(strlen(cmd) + strlen(path)); // Strictly this should be ... - 1 since it includes the the length of '%s'
    sprintf(output, cmd, path);
    return output;
}