You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行C代码后视频格式异常,寻求恢复方案及浏览器显示疑问

问题分析与解决方案

代码核心问题

你运行的C代码存在两个致命问题,直接导致了视频“消失”的现象:

  1. 同名文件直接覆盖:代码中moveFiles(moviesDir, moviesDir)将源目录和目标目录设为同一个,且所有视频文件名相同。rename函数在类Unix系统中,若目标文件已存在会直接覆盖,没有任何提示。后移动的视频会彻底替换先移动的视频,最终只剩最后一个被移动的视频,其余视频被永久覆盖。
  2. 未过滤目标文件:代码会移动子目录下的所有文件,包括CapCut剪辑时生成的.ini、.crc等辅助配置/缓存文件——这些文件文件名不重复,因此全部被保留到主目录,造成“视频消失、杂文件出现”的错觉。

视频能否恢复?

  • 若视频确实被同名覆盖:在类Unix系统中,文件被覆盖后恢复难度极高,除非你有即时的文件系统快照(如Time Machine、Btrfs快照),或使用专业数据恢复工具(但成功率极低,尤其是磁盘已有新写入操作时)。
  • 但根据你的补充说明,浏览器能看到原视频并正常上传,这说明视频大概率未被真正移动/覆盖,是本地文件管理器的缓存问题导致显示异常。你可以通过以下方式验证:
    • 按下F5刷新文件管理器
    • 重启文件管理器
    • 在终端执行命令find ~/Movies -name "*.mov",查找所有MOV格式文件的真实位置

浏览器显示问题说明

浏览器的文件选择器直接读取文件系统的原始元数据,不受本地文件管理器缓存的影响,因此能显示真实存在的文件。浏览器查看不会修改文件格式或属性,它仅读取文件系统的文件名和数据,不会对文件本身造成任何影响。

修正后的代码(避免文件名冲突)

以下代码添加了文件名冲突处理,会给重复文件名自动添加数字后缀(如output_1.mov、output_2.mov),避免覆盖:

#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>

void moveFiles(const char *sourceDir, const char *destDir) {
    DIR *dir = opendir(sourceDir);
    if (dir == NULL) {
        perror("Error opening source directory");
        return;
    }

    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL) {
        if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
            char srcPath[256];
            snprintf(srcPath, sizeof(srcPath), "%s/%s", sourceDir, entry->d_name);

            struct stat fileStat;
            if (stat(srcPath, &fileStat) == 0) {
                if (S_ISREG(fileStat.st_mode)) {
                    char destPath[256];
                    snprintf(destPath, sizeof(destPath), "%s/%s", destDir, entry->d_name);

                    // 处理文件名冲突:自动添加数字后缀
                    int suffix = 1;
                    char tempPath[256];
                    while (access(destPath, F_OK) == 0) {
                        char base[256], ext[256];
                        strcpy(base, entry->d_name);
                        char *dot = strrchr(base, '.');
                        if (dot != NULL) {
                            strcpy(ext, dot);
                            *dot = '\0';
                        } else {
                            strcpy(ext, "");
                        }
                        snprintf(tempPath, sizeof(tempPath), "%s/%s_%d%s", destDir, base, suffix, ext);
                        strcpy(destPath, tempPath);
                        suffix++;
                    }

                    if (rename(srcPath, destPath) != 0) {
                        perror("Error moving file");
                    }
                } else if (S_ISDIR(fileStat.st_mode)) {
                    char subdir[256];
                    snprintf(subdir, sizeof(subdir), "%s/%s", sourceDir, entry->d_name);
                    moveFiles(subdir, destDir);
                }
            }
        }
    }

    closedir(dir);
}

int main() {
    const char *homedir;
    struct passwd *pw = getpwuid(geteuid());
    if (pw != NULL) {
        homedir = pw->pw_dir;
    } else {
        perror("Error getting home directory");
        return 1;
    }

    char moviesDir[256];
    snprintf(moviesDir, sizeof(moviesDir), "%s/Movies", homedir);

    moveFiles(moviesDir, moviesDir);

    printf("Files from subdirectories moved to the 'Movies' directory.\n");

    return 0;
}

内容的提问来源于stack exchange,提问作者Justin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 11:11:00