【问题标题】:Pass system date and time as a filename in C++在 C++ 中将系统日期和时间作为文件名传递
【发布时间】:2014-04-14 15:09:52
【问题描述】:

我想制作一个考勤系统,它将系统日期和时间作为文件的文件名,例如: 平时是这样的

int main () {
time_t t = time(0);   // get time now
struct tm * now = localtime( & t );
cout << (now->tm_year + 1900) << '-'
     << (now->tm_mon + 1) << '-'
     <<  now->tm_mday
     << endl;
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
} 

但我想用系统日期和时间代替 example.txt 我已经通过在程序中包含 ctime 头文件来计算时间,上面的程序只是示例。

【问题讨论】:

  • 那么...你的问题是什么?

标签: c++ fstream ctime


【解决方案1】:

您可以使用strftime() 函数将时间格式化为字符串,它根据您的需要提供了更多的格式化选项。

int main (int argc, char *argv[])
{
     time_t t = time(0);   // get time now
     struct tm * now = localtime( & t );

     char buffer [80];
     strftime (buffer,80,"%Y-%m-%d.",now);

     std::ofstream myfile;
     myfile.open (buffer);
     if(myfile.is_open())
     {
         std::cout<<"Success"<<std::endl;
     }
     myfile.close();
     return 0;
}

【讨论】:

  • 谢谢@Singh 我有疑问为什么你使用 int argc, char *argv[] 作为参数/参数?
  • @ChinmayWankar Ok , int argc, char *argv[] 在这种情况下不需要,您可以忽略它们。您可以了解更多关于他们的信息here
【解决方案2】:
#include <algorithm>
#include <iomanip>
#include <sstream>

std::string GetCurrentTimeForFileName()
{
    auto time = std::time(nullptr);
    std::stringstream ss;
    ss << std::put_time(std::localtime(&time), "%F_%T"); // ISO 8601 without timezone information.
    auto s = ss.str();
    std::replace(s.begin(), s.end(), ':', '-');
    return s;
}

如果你们在国外一起工作,请将std::localtime* 替换为std::gmtime*。

用法例如:

#include <filesystem> // C++17
#include <fstream>
#include <string>

namespace fs = std::filesystem;

fs::path AppendTimeToFileName(const fs::path& fileName)
{
    return fileName.stem().string() + "_" + GetCurrentTimeForFileName() + fileName.extension().string();
}

int main()
{
    std::string fileName = "example.txt";
    auto filePath = fs::temp_directory_path() / AppendTimeToFileName(fileName); // e.g. MyPrettyFile_2018-06-09_01-42-00.log
    std::ofstream file(filePath, std::ios::app);
    file << "Writing this to a file.\n";
}

*请参阅here,了解这些函数的线程安全替代方案。

【讨论】:

    【解决方案3】:

    您可以尝试使用 ostringstream 创建一个日期字符串(就像您使用 cout 所做的那样),然后使用它的 str() 成员函数来检索相应的日期字符串。

    【讨论】:

      【解决方案4】:

      您可以为此目的使用 stringstream 类,例如:

      int main (int argc, char *argv[])
      {
        time_t t = time(0);   // get time now
        struct tm * now = localtime( & t );
        stringstream ss;
      
        ss << (now->tm_year + 1900) << '-'
           << (now->tm_mon + 1) << '-'
           <<  now->tm_mday
           << endl;
      
        ofstream myfile;
        myfile.open (ss.str());
        myfile << "Writing this to a file.\n";
        myfile.close();
        return 0;
      
        return(0);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多