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

用户指定目录间移动dummy.txt失败问题排查

文件移动失败的代码问题排查与修复

问题概述

尝试将dummy.txt从用户输入的源目录移动到目标目录,确认路径和文件均存在,但操作始终失败,原始代码如下:

#include <stdio.h>
#include <time.h>

int main() {
  int ret, ttime, oldfile, newfile, placeholder1, placeholder2;
  int lloop = 0;
  printf("Enter directory of dummy.txt: ");
  scanf("%d", &oldfile);
  printf("Enter new directory: ");
  fflush(stdin);
  scanf("%d", &newfile);
  printf("Starting");

  time_t begin = time(NULL);

  while (lloop != 2) {
    char oldname[] = "%d", oldfile;
    char newname[] = "%d", newfile;
    ret = rename(oldname, newname);

    if (ret == 0) {
      printf("\nFile moved successfully\n");
    } else {
      printf("\nError: unable to move file\n");
    }
    lloop = lloop + 1;
    placeholder1 = oldfile;
    placeholder2 = newfile;
    newfile = placeholder1;
    oldfile = placeholder2;

  }

  time_t end = time(NULL);
  ttime = end - begin;
  printf("Transfer took %d seconds\n", ttime);

  return (0);
}

核心问题分析

  • 数据类型错误:用int存储目录路径,路径本质是字符串,scanf("%d")读取的是整数,完全无法正确获取用户输入的目录。
  • 路径拼接逻辑缺失:循环内定义的oldname和newname只是字符串"%d",没有将用户输入的目录与dummy.txt拼接成完整文件路径,rename函数找不到实际文件。
  • fflush(stdin)未定义行为:该操作在C标准中是未定义的,无法可靠清除输入缓冲区。
  • 无意义的循环逻辑:循环执行两次并交换源/目标路径,不仅无法解决移动问题,还会尝试反向移动,完全不符合需求。
  • 错误信息不明确:仅打印"移动失败",未输出具体错误原因,无法定位问题(如权限不足、路径格式错误等)。

修复后的代码

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

int main() {
    int ret;
    time_t begin, end;
    double ttime;
    char old_dir[256], new_dir[256];
    char old_path[256], new_path[256];

    // 读取源目录,处理换行符
    printf("Enter directory of dummy.txt: ");
    fgets(old_dir, sizeof(old_dir), stdin);
    old_dir[strcspn(old_dir, "\n")] = '\0';

    // 读取目标目录,处理换行符
    printf("Enter new directory: ");
    fgets(new_dir, sizeof(new_dir), stdin);
    new_dir[strcspn(new_dir, "\n")] = '\0';

    // 拼接完整源文件路径
    strncpy(old_path, old_dir, sizeof(old_path) - 1);
    strncat(old_path, "/dummy.txt", sizeof(old_path) - strlen(old_path) - 1);
    old_path[sizeof(old_path)-1] = '\0';

    // 拼接完整目标文件路径
    strncpy(new_path, new_dir, sizeof(new_path) - 1);
    strncat(new_path, "/dummy.txt", sizeof(new_path) - strlen(new_path) - 1);
    new_path[sizeof(new_path)-1] = '\0';

    printf("Starting file move...\n");
    begin = time(NULL);

    // 执行文件移动
    ret = rename(old_path, new_path);
    if (ret == 0) {
        printf("File moved successfully\n");
    } else {
        perror("Error moving file"); // 打印具体错误原因
        exit(EXIT_FAILURE);
    }

    end = time(NULL);
    ttime = difftime(end, begin);
    printf("Transfer took %.2f seconds\n", ttime);

    return 0;
}

关键修改说明

  • 修正数据类型:用char数组存储目录路径,符合字符串类型要求。
  • 正确读取路径:使用fgets读取路径,支持带空格的目录名,同时通过strcspn移除换行符。
  • 拼接完整路径:将用户输入的目录与dummy.txt拼接成完整文件路径,确保rename能定位到目标文件。
  • 移除无效循环:仅执行一次移动操作,避免无意义的反向尝试。
  • 增强错误处理:用perror打印具体错误信息,快速定位问题(如权限不足、路径不存在等)。
  • 优化时间计算:使用difftime计算耗时,避免整数截断导致的精度丢失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 18:55:35