时间修改linux c++

Time modification linux c++

本文关键字:c++ linux 修改 时间      更新时间:2023-10-16

我正试图获得c++中文件夹文件的最后修改日期。但是我不明白我怎么能代替" file.txt"而是一个变量名。

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

project .cpp: In function ' Folder getdir2(std::string)Std::string, Std::string;Std::string) ': project .cpp:325:25:错误:不能转换' const string{也称为const std::basic_string} '到' const char* '为参数' 1 '"int stat(const char*, stat*)"stat (t1,及鲜明);//获取文件属性

代码如下:

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

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

使用std::string.c_str()方法获取C字符串:

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