Reputation: 27
Beginner here, I've been practicing with strings and files, and I've been trying to generate this text file that has the current date as the file name, but for some reason, fopen doesn't generate the file. Any advice?
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
main() {
FILE *fLog;
time_t actualtime;
struct tm *day;
char Date[13];
time(&actualtime);
day = localtime(&actualtime);
strftime(Date, 10, "%x", day);
strcat(Date, ".txt");
printf("%s", Date);
fLog = fopen(Date, "w");
fprintf(fLog, "Hello world");
fclose(fLog);
}
Upvotes: 0
Views: 383
Reputation: 2704
Since you have tagged c++
in this question so I will provide you with C++ solution to it.
#include <iostream>
#include <chrono>
#include <ctime>
#include <string>
#include <fstream>
using namespace std;
int main()
{
auto start = std::chrono::system_clock::now();
std::time_t end_time = std::chrono::system_clock::to_time_t(start);
cout<<ctime(&end_time);
ofstream f1;
f1.open(static_cast<string>(ctime(&end_time)));
}
Here I used this answer to get current date, converted it into string and opened a file name with it.
This will generate a file with current date as it's name, and then you can perform your desired operations with it.
Upvotes: 1