首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >时间修改linux c++

时间修改linux c++
EN

Stack Overflow用户
提问于 2014-06-16 22:25:01
回答 1查看 74关注 0票数 0

我正在试图获取最后修改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的属性。

以下是代码:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2014-06-16 22:27:20

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

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

代码语言:javascript
复制
std::string filename;
...
stat(filename.c_str(), &attrib);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24253159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档