1

我正在使用 Linux GCC c99。

我想知道什么是最好的技术。改变一个字符串。我正在使用 strstr();

我有一个名为“file.vce”的文件名,我想将扩展名更改为“file.wav”。

这是最好的方法吗:

char file_name[80] = "filename.vce";
char *new_file_name = NULL;
new_file_name = strstr(file_name, "vce");
strncpy(new_file_name, "wav", 3);
printf("new file name: %s\n", new_file_name);
printf("file name: %s\n", file_name);

非常感谢您的任何建议,


我使用您的建议编辑了我的答案。你还能看出什么不妥吗?

/** Replace the file extension .vce with .wav */
void replace_file_extension(char *file_name)
{
    char *p_extension;

    /** Find the extension .vce */
    p_extension = strrchr(file_name, '.');
    if(p_extension)
    {
        strcpy(++p_extension, "wav");
    }
    else
    {
        /** Filename did not have the .vce extension */
        /** Display debug information */
    }
}

int main(void)
{
    char filename[80] = "filename.vce";

    replace_file_extension(filename);

    printf("filename: %s\n", filename);
    return 0;
}
4

6 回答 6

3

有几个问题:

char file_name[80] = "filename.vce";
char *new_file_name = NULL;
new_file_name = strstr(file_name, "vce");
strncpy(new_file_name, "wav", 3);
printf("new file name: %s\n", new_file_name);
printf("file name: %s\n", file_name);

只有一个字符串的存储空间,但最后您尝试打印两个不同的字符串。

命名的变量new_file_name实际上指向同一文件名的一部分。

字符串 vce 可能出现在文件名中的任何位置,而不仅仅是扩展名。如果文件名是srvce.vce怎么办?

您可能想找到最后一个 . 字符串中的字符,然后检查它后面是否有预期的扩展名,然后替换该扩展名。请记住,如果您通过修改原始缓冲区来执行此操作,则之后您将无法打印旧字符串。

于 2009-03-27T07:41:35.973 回答
2

而不是搜索 . 或者 vce 其中任何一个都可能在字符串中出现多次,计算字符串的长度并减去 3 以指向扩展名。使用 strncpy 就地替换扩展。

size_t length;
char* pextension;
char file_name[80] = "filename.vce";
printf("file name: %s\n", file_name);    
length = strlen(file_name);
pextension = file_name + length - 3;
strncpy(pextension, "wav", 3);
printf("new file name: %s\n", file_name);
于 2009-03-27T11:51:51.253 回答
1

您必须在末尾手动添加零终止符,new_file_name因为strncpy()在您的情况下不会添加它。

碰巧你已经在正确的地方有零字节,但不能在所有情况下都保证,所以做这样的事情是一个好习惯

new_file_name[3] = '\0';

之后strncpy()

还要注意字符串“vce”可能出现在文件名中。

于 2009-03-27T07:34:49.920 回答
1

您的代码存在一些问题。这是一个不同的看法,更改是内联注释的:

char file_name[80] = "filename.vce";
char *new_file_name = NULL;

printf("old file name: '%s'\n", file_name);
/* Use strrstr(), to find the last occurance, and include the period. */
new_file_name = strrstr(file_name, ".vce");
if(new_file_name != NULL)
{
  /* Use plain strcpy() to make sure the terminator gets there.
   * Don't forget the period.
  */
  strcpy(new_file_name, ".wav");
}
printf("new file name: '%s'\n", file_name);

这仍然可以改进,例如,它不会检查是否有足够的空间来包含新的扩展。但它确实以比戳单个字符更自然的方式终止了字符串。

于 2009-03-27T07:51:31.770 回答
1

我会这样做

char file_name[80] = "filename.vce";
char *pExt;

pExt = strrchr(file_name, ".");
if( pExt )
  strcpy(++pExt, "wav");
else
{
 // hey no extension
}
printf("file name: %s\n", file_name);

您需要在每个 c 程序中进行指针操作。当然你会做更多的检查缓冲区运行等,甚至使用路径特定的功能。

于 2009-03-27T11:15:40.937 回答
1

我很无聊,我没有指出原始代码中的问题,而是编写了自己的代码。出于指导目的,我试图保持清晰。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


/* Replace the last suffix in a filename with a new suffix. Copy the new
   name to a new string, allocated with malloc. Return new string. 
   Caller MUST free new string.

   If old name has no suffix, a period and the new suffix is appended
   to it. The new suffix MUST include a period it one is desired.

   Slashes are interepreted to separate directories in the filename.
   Suffixes are only looked after the last slash, if any.

   */
char *replace_filename_suffix(const char *pathname, const char *new_suffix)
{
    size_t new_size;
    size_t pathname_len;
    size_t suffix_len;
    size_t before_suffix;
    char *last_slash;
    char *last_period;
    char *new_name;

    /* Allocate enough memory for the resulting string. We allocate enough
       for the worst case, for simplicity. */
    pathname_len = strlen(pathname);
    suffix_len = strlen(new_suffix);
    new_size = pathname_len + suffix_len + 1;
    new_name = malloc(new_size);
    if (new_name == NULL)
        return NULL;

    /* Compute the number of characters to copy from the old name. */
    last_slash = strrchr(pathname, '/');
    last_period = strrchr(pathname, '.');
    if (last_period && (!last_slash || last_period > last_slash))
        before_suffix = last_period - pathname;
    else
        before_suffix = pathname_len;

    /* Copy over the stuff before the old suffix. Then append a period
       and the new suffix. */
#if USE_SPRINTF
    /* This uses snprintf, which is how I would normally do this. The
       %.*s formatting directive is used to copy a specific amount
       of text from pathname. Note that this has the theoretical
       problem with filenames larger than will fit into an integer. */
    snprintf(new_name, new_size, "%.*s%s", (int) before_suffix, pathname,
             new_suffix);
#else
    /* This uses memcpy and strcpy, to demonstrate how they might be
       used instead. Much C string processing needs to be done with
       these low-level tools. */
    memcpy(new_name, pathname, before_suffix);
    strcpy(new_name + before_suffix, new_suffix);
#endif

    /* All done. */
    return new_name;
}


int main(int argc, char **argv)
{
    int i;
    char *new_name;

    for (i = 1; i + 1 < argc; ++i) {
        new_name = replace_filename_suffix(argv[i], argv[i+1]);
        if (new_name == NULL) {
            perror("replace_filename_suffix");
            return EXIT_FAILURE;
        }
        printf("original: %s\nsuffix: %s\nnew name: %s\n",
               argv[i], argv[i+1], new_name);
        free(new_name);
    }
    return 0;
}
于 2009-03-27T12:38:49.147 回答