C++ 如何使用字符串中保存的文件路径创建带时间戳的目录
C++ How do I create a timestamped directory using a filepath held in a string?
我正在尝试设置一个程序,该程序可以在每次用于填充数据时创建一个新目录。我希望文件夹的名称是创建时的时间戳。我已经编写了一个创建时间戳并将其作为字符串返回的函数。
string timestamp() {
//create a timecode specific folder
// current date/time based on current system
time_t now = time(0);
struct tm timeinfo;
localtime_s(&timeinfo, &now);
// print various components of tm structure.
cout << "Year: " << 1900 + timeinfo.tm_year << endl;
int Y = 1900 + timeinfo.tm_year;
cout << "Month: " << 1 + timeinfo.tm_mon << endl;
int M = 1 + timeinfo.tm_mon;
cout << "Day: " << timeinfo.tm_mday << endl;
int D = timeinfo.tm_mday;
cout << "Time: " << 1 + timeinfo.tm_hour << ":";
int H = timeinfo.tm_hour;
cout << 1 + timeinfo.tm_min << ":";
int Mi = timeinfo.tm_min;
cout << 1 + timeinfo.tm_sec << endl;
int S = 1 + timeinfo.tm_sec;
string timestampStr;
stringstream convD, convM, convY, convH, convMi, convS;
convD << D;
convM << M;
convY << Y;
convH << H;
convMi << Mi;
convS << S;
cout << "Timestamp:" << endl;
timestampStr = convD.str() + '.' + convM.str() + '.' + convY.str() + '-' + convH.str() + ':' + convMi.str() + ':' + convS.str();
cout << timestampStr << endl;
return timestampStr;
}
这个位工作正常,给了我一个带有当前时间戳的字符串。我还有第二个字符串,其中包含文件夹位置的路径,我将其组合在一起以在字符串中提供完整路径和新文件夹名称,例如"C:\\Users\\Daniel\\Documents\\VS17\\25.5.2017-16:47:51"
我知道我可以使用
CreateDirectory(direc, NULL);
当我有一个文件路径时,创建一个目录,就像我的字符串中保存的文件路径一样,但采用 LPCWSTR 格式。所以我的问题是
如何将我的字符串转换为 LPCWSTR 格式以在创建目录中使用
我只是想念其他方式吗
由于您的文件夹路径字符串是基于char
的字符串,因此只需直接使用CreateDirectoryA()
,而不是使用基于TCHAR
的CreateDirectory()
(显然正在映射到项目中的CreateDirectoryW()
(,例如:
string direc = "C:\Users\Daniel\Documents\VS17\" + timestamp();
CreateDirectoryA(direc.c_str(), NULL);
相关文章:
- C++:floor unix时间戳到UTC月份
- 如何在c++中录制具有精确帧时间戳的视频
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- Google protobuf 时间戳未声明标识符,在 Windows 上具有C++
- 在多个时间戳处执行函数
- 以天C++为单位的两个时间戳之间的差异
- 获取 QInputEvent 在 Qt 4.8 中被放入 QEventLoop 队列时的时间戳
- 在 C++ 中为文件名添加时间戳
- 如何从远程 SFTP 服务器获取 HH-MM-SS 时间戳格式的文件列表
- 如何将消息时间戳写入日志文件?
- 将时间戳打印到流的最简单方法
- 读取悖论时间戳字段
- 如何使用微秒创建时间戳
- C++ 如何使用字符串中保存的文件路径创建带时间戳的目录
- 如何从 UTC 时间戳的组件(Y,M,D,H,M,S,MS)创建system_clock::time_point
- 使用localtime_s创建时间戳?并使用asctime_s C++
- 如何时间戳签名创建
- 制作旋转器的功能,每天每分钟创建一个时间戳
- 有没有办法从GUID中找到时间戳(创建时间)?
- 创建一个标题中带有时间戳的文本文件