鱼C论坛

 找回密码
 立即注册
查看: 2744|回复: 5

[已解决]file_system_创建文件_请教

[复制链接]
发表于 2021-6-18 09:07:59 | 显示全部楼层 |阅读模式
60鱼币
版上的大大你们好

使用C语言

请教一下
我要创建100个文件
每次执行指创建一个
档案名称依照流水号
1~100
例如像这样

file_001.txt
file_002.txt
....
file_100.txt

我目前可以创建第一个
       
但是无法创建第二个

使用的函示库 stdio.h

这个问题我头痛了好段时间了

请大神们指导小弟......
最佳答案
2021-6-18 09:08:00
#include <stdio.h>
#include <stdlib.h>                //malloc
#include <string.h>                //memset

#define MAX_FILE_SIZE        100

//EXIT_FAILURE 和 EXIT_SUCCESS 是定义于 stdlib 中的
//EXIT_FAILURE = 1 | EXIT_SUCCESS = 0

int CreateTxtFile(char* name)
{
        FILE* file = NULL;
        fopen_s(&file, name, "w+");
        if (file == NULL)
                return EXIT_FAILURE;        //创建失败

        fclose(file);
        return EXIT_SUCCESS;
}

int CreateTxtFileByTimes(int time)
{
        char* filename = (char*)malloc(MAX_FILE_SIZE);
        if (filename == NULL)
                return EXIT_FAILURE;

        for (int i = 0; i < time; ++i)
        {
                memset(filename, 0, MAX_FILE_SIZE);
                sprintf_s(filename, MAX_FILE_SIZE, "file_%03d.txt", i + 1);
                if (CreateTxtFile(filename) == EXIT_FAILURE)
                {
                        free(filename);
                        return EXIT_FAILURE;
                }
        }

        free(filename);
        return EXIT_SUCCESS;
}

int main()
{
        CreateTxtFileByTimes(100);

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-18 09:08:00 | 显示全部楼层    本楼为最佳答案   
#include <stdio.h>
#include <stdlib.h>                //malloc
#include <string.h>                //memset

#define MAX_FILE_SIZE        100

//EXIT_FAILURE 和 EXIT_SUCCESS 是定义于 stdlib 中的
//EXIT_FAILURE = 1 | EXIT_SUCCESS = 0

int CreateTxtFile(char* name)
{
        FILE* file = NULL;
        fopen_s(&file, name, "w+");
        if (file == NULL)
                return EXIT_FAILURE;        //创建失败

        fclose(file);
        return EXIT_SUCCESS;
}

int CreateTxtFileByTimes(int time)
{
        char* filename = (char*)malloc(MAX_FILE_SIZE);
        if (filename == NULL)
                return EXIT_FAILURE;

        for (int i = 0; i < time; ++i)
        {
                memset(filename, 0, MAX_FILE_SIZE);
                sprintf_s(filename, MAX_FILE_SIZE, "file_%03d.txt", i + 1);
                if (CreateTxtFile(filename) == EXIT_FAILURE)
                {
                        free(filename);
                        return EXIT_FAILURE;
                }
        }

        free(filename);
        return EXIT_SUCCESS;
}

int main()
{
        CreateTxtFileByTimes(100);

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-18 09:40:00 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-18 09:59:41 | 显示全部楼层
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    system("rm -r temp");
    system("mkdir temp");
    char command[64];
    for(size_t i = 1; i <= 100; ++i) {
        snprintf(command, 64, "echo 'count: %lu' > temp/file_%.3lu.txt", i, i);
        system(command);
    }
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-18 13:04:08 | 显示全部楼层

好家伙,我直呼好家伙
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-18 13:46:06 | 显示全部楼层
yuxijian2020 发表于 2021-6-18 13:04
好家伙,我直呼好家伙

^_^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 14:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表