c - 如何从文件名中删除扩展名?

标签 c file-extension

我想去掉文件名的最后三个字符并获取其余的?

我有这个代码:

char* remove(char* mystr) {

    char tmp[] = {0};
    unsigned int x;

    for (x = 0; x < (strlen(mystr) - 3); x++)
        tmp[x] = mystr[x];

    return tmp;
}

最佳答案

尝试:

char *remove(char* myStr) {
    char *retStr;
    char *lastExt;
    if (myStr == NULL) return NULL;
    if ((retStr = malloc (strlen (myStr) + 1)) == NULL) return NULL;
    strcpy (retStr, myStr);
    lastExt = strrchr (retStr, '.');
    if (lastExt != NULL)
        *lastExt = '\0';
    return retStr;
}

您必须自己释放返回的字符串。它只是找到字符串中的最后一个 . 并将其替换为空终止符。它将通过返回 NULL 来处理错误(传递 NULL 或内存不足)。

它不会像 /this.path/is_bad 这样的东西工作,因为它会在非文件部分找到 . 但你也可以处理这个执行 /strrchr,或者无论您的路径分隔符是什么,并确保它的位置是 NULL 之前。 位置。


这个问题的更通用的解决方案可能是:

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

// remove_ext: removes the "extension" from a file spec.
//   myStr is the string to process.
//   extSep is the extension separator.
//   pathSep is the path separator (0 means to ignore).
// Returns an allocated string identical to the original but
//   with the extension removed. It must be freed when you're
//   finished with it.
// If you pass in NULL or the new string can't be allocated,
//   it returns NULL.

char *remove_ext (char* myStr, char extSep, char pathSep) {
    char *retStr, *lastExt, *lastPath;

    // Error checks and allocate string.

    if (myStr == NULL) return NULL;
    if ((retStr = malloc (strlen (myStr) + 1)) == NULL) return NULL;

    // Make a copy and find the relevant characters.

    strcpy (retStr, myStr);
    lastExt = strrchr (retStr, extSep);
    lastPath = (pathSep == 0) ? NULL : strrchr (retStr, pathSep);

    // If it has an extension separator.

    if (lastExt != NULL) {
        // and it's to the right of the path separator.

        if (lastPath != NULL) {
            if (lastPath < lastExt) {
                // then remove it.

                *lastExt = '\0';
            }
        } else {
            // Has extension separator with no path separator.

            *lastExt = '\0';
        }
    }

    // Return the modified string.

    return retStr;
}

int main (int c, char *v[]) {
    char *s;
    printf ("[%s]\n", (s = remove_ext ("hello", '.', '/'))); free (s);
    printf ("[%s]\n", (s = remove_ext ("hello.", '.', '/'))); free (s);
    printf ("[%s]\n", (s = remove_ext ("hello.txt", '.', '/'))); free (s);
    printf ("[%s]\n", (s = remove_ext ("hello.txt.txt", '.', '/'))); free (s);
    printf ("[%s]\n", (s = remove_ext ("/no.dot/in_path", '.', '/'))); free (s);
    printf ("[%s]\n", (s = remove_ext ("/has.dot/in.path", '.', '/'))); free (s);
    printf ("[%s]\n", (s = remove_ext ("/no.dot/in_path", '.', 0))); free (s);

    return 0;
}

这会产生:

[hello]
[hello]
[hello]
[hello.txt]
[/no.dot/in_path]
[/has.dot/in]
[/no]

关于c - 如何从文件名中删除扩展名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2736753/

相关文章:

c函数指针解释

c - C多维数组是否连续无孔?

javascript - 使用不同的文件扩展名运行 node.js

java - 即使文件扩展名已更改,如何识别文件类型?

具有不同扩展名的Python保存文件

c - 前一个数组追加到当前数组

c - Linux C 编程 : How to get a device's partition infomation?

c++ - 为什么定义#if 0 && (_MSC_VER > 1000)?

unix - 通过 unix shell 命令查找给定文件扩展名的首选应用程序

cuda - CUDA源文件的扩展名为.cu。头文件会得到什么?