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

C语言如何获取格式化生成的文件名?现有代码优化咨询

解决方案

现有实现存在两个核心问题:

  1. 共用单个fileName数组存储文件名,三次创建文件操作会依次覆盖数组内容,最终仅保留最后一次生成的随机类文件名,无法同时留存三个文件的名称供后续使用
  2. 排序分支中直接向fopen传入带格式化占位符的字符串,fopen没有字符串格式化能力,无法解析%08d占位符,会直接把占位符当作文件名的一部分查找,无法找到对应文件

本方案完全不需要改动现有创建函数的返回类型,仅调整main.c逻辑即可解决问题,同时附带可选用的代码优化项:

  • 在main函数中声明三个独立的字符数组,分别存储有序、逆序、随机三类文件的名称
  • 创建文件时分别传入对应的数组存储文件名,后续读取文件时直接使用对应数组的内容即可
  • 补充随机数种子初始化、文件打开失败的异常判断,移除头文件全局变量避免编译冲突

修改后的main.c

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

int main(void) {
  int range, choice;
  // 分别存储三类文件的文件名
  char orderedName[255], invertedName[255], randomicName[255];
  // 初始化随机数种子,保证每次生成的随机数序列不同
  srand(time(NULL));

  do {
    printf("菜单\n\n");
    printf("1 - 创建文件 \n");
    printf("2 - 使用冒泡排序 \n");
    printf("3 - 使用插入排序 \n");
    printf("4 - 退出程序 \n");

    scanf("%d", &choice);

    switch(choice) {
      case 1:
        printf("请输入文件大小: ");
        scanf("%d", &range);
        // 分别传入对应数组存储文件名
        createOrderedFile(range, orderedName, NULL);
        createInvertedFile(range, invertedName, NULL);
        createRandomicFile(range, randomicName, NULL);
        printf("三类文件已生成:\n%s\n%s\n%s\n", orderedName, invertedName, randomicName);
        break;
      case 2: {
        // 直接使用之前存储的随机文件名打开
        FILE *randomicFp = fopen(randomicName, "r");
        if (randomicFp == NULL) {
          printf("请先创建文件再执行排序操作\n");
          break;
        }
        // 此处补充冒泡排序读取文件、排序的业务逻辑
        printf("正在读取%s进行冒泡排序\n", randomicName);
        fclose(randomicFp);
        break;
      }
      case 3: {
        // 同理使用对应文件名即可
        FILE *randomicFp = fopen(randomicName, "r");
        if (randomicFp == NULL) {
          printf("请先创建文件再执行排序操作\n");
          break;
        }
        // 此处补充插入排序读取文件、排序的业务逻辑
        printf("正在读取%s进行插入排序\n", randomicName);
        fclose(randomicFp);
        break;
      }
      case 4:
        printf("程序退出\n");
        break;
      default:
        printf("请输入有效的选项\n");
    }
  } while(choice != 4);
  return 0;
}

优化后的generateFiles.h(可选,不影响原有功能)

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

// 移除全局变量INDEX,改为函数内局部变量,避免多文件编译冲突

void createOrderedFile(int range, char fileName[255], FILE *ordered) {
  int index;
  sprintf(fileName, "Ordered%08d.txt", range);
  ordered = fopen(fileName, "w");
  if (ordered == NULL) return;
  for (index = 1; index <= range; index++) {
    fprintf(ordered, "%d\n", index);
  }
  fclose(ordered);
}

void createInvertedFile(int range, char fileName[255], FILE *inverted) {
  int index;
  sprintf(fileName, "Inverted%08d.txt", range);
  inverted = fopen(fileName, "w");
  if (inverted == NULL) return;
  for (index = range; index >= 1; index--) {
    fprintf(inverted, "%d\n", index);
  }
  fclose(inverted);
}

void createRandomicFile(int range, char fileName[255], FILE *randomic) {
  int index;
  sprintf(fileName, "Randomic%08d.txt", range);
  randomic = fopen(fileName, "w");
  if (randomic == NULL) return;
  for (index = 1; index <= range; index++) {
    fprintf(randomic, "%d\n", rand() % range + 1);
  }
  fclose(randomic);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.15 15:36:04