【发布时间】:2021-01-22 09:27:54
【问题描述】:
首先:我知道,以前也出现过一些类似的问题,但它对我不起作用。
我需要在 C 中创建一个名为 "year/month/day_hour 的 .txt 文件em> :分钟
我的代码是:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main(){
int hours, minutes, seconds, day, month, year;
char fileSpec [255];
//TIME for file name
time_t now;
time(&now);
printf("Today is : %s", ctime(&now));
struct tm *local = localtime(&now);
hours = local->tm_hour;
minutes = local->tm_min;
seconds = local->tm_sec;
day = local->tm_mday;
month = local->tm_mon + 1;
year = local->tm_year + 1900;
//Time block end
//create file
FILE * fPointer;
snprintf( fileSpec, "%04d_%02d_%02d_%02d:%02d.txt",year,month,day,hours,minutes); //From another post. I do not exactly know the syntax
fPointer = fopen(fileSpec,"w");
fprintf(fPointer ,"Date and Time of creation: %d/%d/%d %d:%d:%d\n",day,month,year,hours,minutes,seconds);
}
对不起我奇怪的格式:/
我还是个初学者。
编辑:整个代码是:https://pastebin.com/78fSRJgx
【问题讨论】:
-
您应该说明您的实际问题是什么。但我猜它源于在文件名中使用 / 。这不起作用,因为它可以分隔目录。
-
你需要
fclose(fPointer);