c语言如何使用mkdir函数创建以日期为名的文件夹,并且该文件夹下边再创建两个不同位置命名的文件夹,在该文件夹下边再存放照片
时间: 2023-11-28 17:50:04 浏览: 61
你可以使用time.h头文件中的函数获取当前系统时间,然后将时间作为文件夹的名称。具体实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
int main() {
time_t t = time(NULL);
struct tm *tm = localtime(&t);
char date[20];
sprintf(date, "%04d-%02d-%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
mkdir(date, 0777); // 创建以日期为名称的文件夹
char dir1[100], dir2[100];
sprintf(dir1, "./%s/dir1", date);
sprintf(dir2, "./%s/dir2", date);
mkdir(dir1, 0777); // 在该文件夹下创建两个不同位置命名的文件夹
mkdir(dir2, 0777);
char photo1[100], photo2[100];
sprintf(photo1, "./%s/dir1/photo1.jpg", date);
sprintf(photo2, "./%s/dir2/photo2.jpg", date);
// 在文件夹下边再存放照片
FILE *f1 = fopen(photo1, "wb");
FILE *f2 = fopen(photo2, "wb");
// 写入照片数据
// ...
fclose(f1);
fclose(f2);
return 0;
}
```
在该程序中,首先使用time函数获取当前系统时间,并将其转换为字符串格式作为文件夹名称。然后使用mkdir函数创建该文件夹。接着使用sprintf函数构造两个不同位置命名的文件夹,在该文件夹下分别创建这两个文件夹。最后使用fopen函数打开照片文件,并写入照片数据。