将 char** 复制到作为函数参数传递的 char*

问题描述 投票:0回答:1

我在C中创建了一个更改文件扩展名的个人函数。

一个,也许不止一个,问题是我不知道如何将

source
字符复制到
buff
变量。我遇到分段错误,请参阅示例:

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

int get_dot_pos (const char* str, int* size)
{
    int npos = 0;
    int n = 0;
    for (int x = 0; *str != '\0'; x++)
    {
        if (*str == '.')
            npos = x;
        str++;
        n++;
    }
    *size = n;
    return npos;
}

int replace_ext (const char* source, int source_size, int dot_pos, const char* new_ext, char** buff)
{
    *buff = malloc(source_size+1);
    int x = 0;
    int go_on = 1;
    while (go_on)
    {
        if (x != dot_pos)
        {
            **buff = *source; // sg fault here :(
            *buff++;
            source++;
            x++;
        }
        else
        {
            for(; *new_ext; new_ext++)
            {
                **buff = *new_ext;
                *buff++;
                x++;
            }
            *buff = '\0';
            go_on = 0;
        }
    }
    return x;
}


int main()
{
    const char* str1 = "/bar/foo/buz/rolitas.mp3";
    int size = 0;
    int npos = get_dot_pos (str1, &size);
    char* str2;
    replace_ext (str1, size, npos+1, "ogg", &str2);
    printf("Before: %s\n", str1);
    printf("After: %s", str2);
    free (str2);
    return 0;
}

有什么想法吗?

c pointers
1个回答
0
投票

看起来很有趣。给你:

char * change_file_ext( char * path, const char * extension );
// Function
//   Change the file path's extension.
// 
// Arguments
//   path 
//     MUST be an array with enough space to change to the new extension!
//     Need not have an initial extension.
//     May be any valid path. No validation is performed.
//     Must not be NULL.
//
//   extension 
//     Non-empty strings change the extension.
//       char path[MAX_PATH] = "hello.z";
//       change_file_ext( path, ".ext" ) --> "hello.ext"
//       change_file_ext( path, "ext"  ) --> "hello.ext"
//       change_file_ext( path, "."    ) --> "hello."
//     The empty string removes the extension:
//       change_file_ext( path, ""     ) --> "hello"
//     Must not be NULL.
//
// Returns
//   path
//

char * replace_file_ext( const char * path, const char * extension );
// Function
//   Change the file path's extension, returning a newly-allocated
//   string.
//
//   This otherwise works just like change_file_ext().
//
//   Don't forget to free() the result.
//
// Returns
//   path or NULL on allocation failure
//
#include <iso646.h>
#include <stdlib.h>
#include <string.h>

char * change_file_ext( char * path, const char * extension )
{
    size_t n = strlen( path ), N = n;
    while (n--)
        switch (path[n])
        {
            case '.':            N = n;
            case '/': case '\\': break;
        }
    if (!N or (path[N-1] != '.')) path[N] = '.';
    strcpy( path + N + (*extension and (*extension != '.')), extension );
    return path;
}

char * replace_file_ext( const char * path, const char * extension )
{
    size_t n = strlen( path ), N = n;
    while (n--)
        switch (path[n])
        {
            case '.':            N = n;
            case '/': case '\\': break;
        }
    char * new_path = malloc( N + strlen( extension ) + 1 );
    if (!new_path) return NULL;
    strncpy( new_path, path, N );
    new_path[N] = '.';
    strcpy( new_path + N + (*extension and (*extension != '.')), extension );
    return new_path;
}

经过彻底测试。

使用示例:

int main(void)
{
    const char* filename1 = "/bar/foo/buz/rolitas.mp3";
    char* filename2 = replace_file_ext( filename1, "ogg" );
    printf("Before: %s\n", filename1);
    printf("After: %s", filename2);
    free (filename2);
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.