不会覆盖提供的结构的本地时间替代项
localtime alternative that won't overwrite the supplied struct
本质上,我要做的是检查文件的最后访问时间,并将其与字符串进行比较。这是相关的区块:
struct stat file;
char timeStr[ 100 ];
stat(nodes.at(0), &file);
strftime(timeStr, 100, "%H:%M:%S-%m/%d/%y", localtime(&file.st_atime)); /* problem */
CCD_ 1是文件路径的向量;我不确定它是否相关,但我会包括我用来设置nodes
:的代码
vector<char*> nodes;
DIR *dir;
struct dirent *cur
if((dir = opendir(searchPath.c_str())) == NULL) {
cout << "Error opening search path. Are you sure '"
<< searchPath.c_str() << "' is a valid search path?" << endl;
return 0;
}
while((cur = readdir(dir)) != NULL) {
if(string(cur->d_name) == "." || string(cur->d_name) == "..") continue;
nodes.push_back(cur->d_name);
}
closedir(dir);
其中,searchPath
是用户输入的字符串。
问题是:当"problem"行运行时,nodes
上会出现一个垃圾向量。我想知道我是否可以在不把nodes
变成垃圾的情况下完成这项任务。
既然这是家庭作业,而且你可能知道我不习惯C++,那么朝着正确的方向努力就会得到"接受"。
谢谢。
它与strftime
调用无关,而是与以下事实有关(从这里开始):
readdir()返回的指针指向的数据可能会被同一目录流上另一个对readdir(()的调用所覆盖。
由于您只是简单地推送一个字符指针,该指针指向可能被随后对readdir
的调用覆盖的数据,因此很可能会以垃圾告终。
您可能可以通过使用C字符串的副本来修复它,比如:
nodes.push_back (strdup (cur->d_name)); // plus error handling if need be.
而且,如果您的实现没有strdup
(它不是标准的一部分),您可以使用我的(在这里找到)。
nodes.push_back(cur->d_name);
您在向量中存储的指针会立即变为无效(cur
在下一次nodes
0或closedir
调用之前一直有效)。最好的解决方案是编写您想要的代码——使nodes
成为string
s的向量。最简单的解决方案:
nodes.push_back(strdup(cur->d_name));
相关文章:
- C++ 中具有 O(1) 搜索时间复杂度的数据结构
- 将字符数组转换为时间结构
- 保持排序的数据结构,允许log N插入时间,并且可以返回我在log N中查找的元素的索引
- 结构时间规范可以重复使用纳米睡眠吗?
- 将结构时间规范添加到类(似乎没有其他类型的)会导致EXEC_BAD_ACCESS
- 如何制作递归编译时间变异结构模板
- 哪种控制结构所需的时间复杂度更低?
- 在输入结构对象的输入过程中运行时间错误
- C 自定义时间日期结构到UTC时期
- 结构成员的地址作为编译时间常数之间的差异
- 使用结构显示军事时间,并将一秒钟的时间添加到用户输入(C )中
- 是否有具有对数时间插入、删除和查找(带距离)的排序数据结构
- C++中结构的存储持续时间和成员初始化
- 向C++中的任何日期结构添加一定时间量
- 如何在另一个结构中保存 GetLocalTime 时间
- 在C++中将数据和时间分配给文件时间结构
- 如何从乌龙龙毫秒创建系统时间结构
- VC++ 中的文件时间结构
- 正在查找具有tm以外的成员对象的另一个时间结构
- c++时间结构错误