【问题标题】:Time modification linux c++时间修改linux c++
【发布时间】:2014-06-16 22:25:01
【问题描述】:

我正在尝试获取 C++ 中文件夹文件的最后修改日期。 但我不明白如何替换“afile.txt”而是一个变量名。

当我用其他东西替换“afile.txt”时,我得到了这个错误:

proj.cpp: 在函数'Folder getdir2(std::string, 标准::向量>&,标准::字符串,标准::字符串, std::string)': proj.cpp:325:25: 错误: 无法转换'const string {aka const std::basic_string}’ 到 ‘const char*’ 用于参数 ‘1’ 到‘int stat(const char*, stat*)’ 统计(t1,&属性); // 获取 afile.txt 的属性

代码如下:

struct tm* clock;       // create a time structure
        struct stat attrib;     // create a file attribute structure    
        stat("afile.txt", &attrib);   // get the attributes of afile.txt
        clock = gmtime(&(attrib.st_mtime)); // Get the last modified time and put it into the time structure

【问题讨论】:

标签: c++ time


【解决方案1】:

您似乎正试图将std::string 传递给statstat 是一个 C 函数,因此只接受 const char *(一个“C”字符串)作为输入。

使用std::string.c_str()方法得到一个C字符串:

std::string filename;
...
stat(filename.c_str(), &attrib);

【讨论】:

    猜你喜欢
    • 2017-03-30
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 2012-05-13
    • 2023-03-26
    • 2023-03-09
    • 2014-10-11
    • 1970-01-01
    相关资源
    最近更新 更多