c++ - 在 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 数组中的第四个值。我已经尝试过在网上查找的各种解决方案,但现在我迷路了,需要帮助。最值得注意的错误是“表达式必须具有整数或无范围枚举类型”,我已经用谷歌搜索了它,但我仍然无法完全理解它与我的问题的关系以及如何解决它。谢谢你。
解决方案
推荐阅读
- python - 在两个跨度之间抓取数据
- ios - SwiftUI - 我可以在 iPhone 上显示标签栏,在 iPad 上显示侧边栏吗?
- windows - 如何在不使用命令的情况下安装带参数的 msi
- qt - 为什么要开2个窗户?
- javascript - for循环内的nodejs请求函数
- sql - 更有效的查询来获取前几个月的最后一条记录(如果存在)或本月的第一条记录(如果不存在)
- django - Oracle 数据库的 Django 迁移问题
- python - Python 3x:使用 python xml.etree 解析带有命名空间的 xml 文件
- javascript - 回调为 num_rows 并在语句满足时中断
- reactjs - 收到 `children` 属性的 NaN。如果这是预期的,请将值转换为字符串