|
|
60鱼币
版上的大大你们好
使用C语言
请教一下
我要创建100个文件
每次执行指创建一个
档案名称依照流水号
1~100
例如像这样
file_001.txt
file_002.txt
....
file_100.txt
我目前可以创建第一个
但是无法创建第二个
使用的函示库 stdio.h
这个问题我头痛了好段时间了
请大神们指导小弟......
- #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;
- }
复制代码
|
|