gpt4 book ai didi

c++ - 将名称保存为当前时间日期的文件

转载 作者:行者123 更新时间:2023-11-28 00:16:54 29 4
gpt4 key购买 nike

这是我从网上各种教程中找到的一些代码:

#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;
}

任何人都可以指导我如何以这样的时间日期格式保存此文件:

yyyymmddhhmmss

我相信答案是基于这段代码,但我不确定如何实现它:

time_t rawTime

最佳答案

我建议对文件使用 C++ 流。所以例子可以是这样的:

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;

关于c++ - 将名称保存为当前时间日期的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29623161/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com