首页 > 解决方案 > 在 C++ 中组合字符串以重命名文件以显示日期

问题描述

我是 C++ 新手。我正在尝试编写一个小程序,该程序将从我的桌面获取一个 .txt 文件,将其排序到一个文件夹中并将其重命名为当前日期。我的想法是结合三个字符串,第一个字符串是指向我要移动文件的主目录的方向,第二个字符串是我的母语中的月份名称,第三个字符串是代表日期的字符串 dd -mm.txt 格式。

#include <iostream>
#include <filesystem>
#include <ctime>
using namespace std;

int main() {
    int result;
    std::time_t t = std::time(0);
    std::tm* now = std::localtime(&t);
    char date_string[100];
    strftime(date_string, 50, "\\%e-%m.txt", now);
    string mnths[12] = { "Januar", "Februar","Mart","April","Maj","Jun","Jul","Avgust","Septembar","Oktobar","Novembar","Decembar" };

    char strt[] = "C:\\Users\\B\\Desktop\\New.txt";
    char fnsh[] = "C:\\Users\\B\\Desktop\\Journal\\2020\\"+ mnths[date_string[month]]+ date_string;

    result = rename(strt, fnsh);
    if (result == 0)
        puts("File successfully renamed");
    else
        perror("Error renaming file");
    return 0;
};

对于第二个字符串,以 April 为例,我想获取 mnths 数组中的第四个值。我已经尝试过在网上查找的各种解决方案,但现在我迷路了,需要帮助。最值得注意的错误是“表达式必须具有整数或无范围枚举类型”,我已经用谷歌搜索了它,但我仍然无法完全理解它与我的问题的关系以及如何解决它。谢谢你。

标签: c++stringtime

解决方案


推荐阅读