Jimmy
Jimmy

Reputation: 12517

Save file with name as current timedate

This is some code I have found from various tutorials online:

#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string>

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

int main(void) {
    CURL *curl;
    FILE *fp;
    CURLcode res;
    char *url = "http://localhost/aaa.txt";
    char outfilename[FILENAME_MAX] = "C:\\bbb.txt";
    curl = curl_easy_init();
    if (curl) {
        fp = fopen(outfilename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);
    }
    return 0;
}

Can anyone give me some guidance on how I would save this file in a timedate format like this:

yyyymmddhhmmss

I believe the answer is based on this bit of code but I'm not sure how to implement it:

time_t rawTime

Upvotes: 0

Views: 76

Answers (1)

gomons
gomons

Reputation: 1976

I suggest use C++ streams for files. So example can look like this:

std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
std::stringstream filename_stream;
filename_stream << std::put_time(&tm, "%Y%m%d%H%M%S");
std::string file_name;
filename_stream >> file_name;
std::ofstream file(file_name);
file << "File content" << std::endl;

Upvotes: 3

Related Questions