|
通过 C 语言字符串处理函数 来区分路径下文件名的后缀,比如用 strrchr() 找到最后一个 '.',然后比较扩展名。
#include <stdio.h>
#include <string.h>
void check_file_extension(const char *path) {
// 找到最后一个 '.' 的位置
const char *dot = strrchr(path, '.');
if (!dot || dot == path) {
printf("文件没有扩展名: %s\n", path);
return;
}
if (strcmp(dot + 1, " mp3") == 0) {
printf("这是 .mp3 文件: %s\n", path);
}
else if (strcmp(dot + 1, "mp4") == 0) {
printf("这是 .mp4 文件: %s\n", path);
}
else {
printf("未知扩展名: %s\n", dot + 1);
}
}
|
|