我正在试图获取最后修改C++文件夹文件的日期。但我不明白如何才能将"afile.txt“替换为变量名。
当我将"afile.txt“替换为其他东西时,我得到了以下错误:
proj.cpp:在函数‘getdir2(std::getdir2,std::getdir2 >&,std:: string,std::getdir2,std::proj.cpp:325:25):proj.cpp:325:25:无法将参数’1‘的const *转换为’const char*‘,用于参数’1‘int stat(const char*,stat*)’stat(t1 &attrib);//获取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发布于 2014-06-16 22:27:20
似乎您正在尝试将一个std::string传递给stat。stat是一个C函数,因此只接受const char * ( "C“字符串)作为输入。
使用.c_str()方法std::string获得一个C字符串:
std::string filename;
...
stat(filename.c_str(), &attrib);https://stackoverflow.com/questions/24253159
复制相似问题