- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有以下代码抽象:
DIR *d;
struct dirent *sd;
d = opendir("SOME DIRECTORY");
// handle errors
sd = readdir(d);
while(sd != NULL){
printf("(%s, %s)\n", sd->d_name, sd->the_next_d_name);
sd = readdir(d);
}
closedir(dir);
我对 c 有点陌生,正在详细了解 man
页面。我只是想了解如何在循环体中访问目录的两个条目。该代码使用一些假名来表达这个想法。另外,如果有奇数个条目,我希望最后一对有一个与 NULL
无论如何,谢谢。
最佳答案
主要问题是 readdir()
函数在第二次调用时不会保留先前返回的值:
The application shall not modify the structure to which the return value of
readdir()
points, nor any storage areas pointed to by pointers within the structure. The returned pointer, and pointers within the structure, might be invalidated or the structure or the storage areas might be overwritten by a subsequent call toreaddir()
on the same directory stream. They shall not be affected by a call toreaddir()
on a different directory stream.
因此,您必须复制每对条目中第一个条目的名称。想必,您有 strdup()
可用,所以这是最小的痛苦。 (如果您没有 strdup()
,则很容易编写自己的版本 — 7 行,包括仅带有大括号的行。)
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "stderr.h"
int main(int argc, char **argv)
{
err_setarg0(argv[0]);
if (argc > 2)
err_usage("[directory]");
char *dir = ".";
if (argc == 2)
dir = argv[1];
DIR *dp = opendir(dir);
if (dp == NULL)
err_syserr("failed to open directory '%s': ", dir);
char *name_0 = 0;
int counter = 0;
struct dirent *name;
while ((name = readdir(dp)) != NULL)
{
if (++counter % 2 == 1)
{
if ((name_0 = strdup(name->d_name)) == NULL)
err_syserr("failed to duplicate name '%s': ", name->d_name);
}
else
{
printf("(%s, %s)\n", name_0, name->d_name);
free(name_0);
name_0 = NULL;
}
}
if (counter % 2 == 1)
printf("(%s, %s)\n", name_0, "NULL");
free(name_0);
closedir(dp);
return 0;
}
(stderr.h
中声明的函数均以 err_
开头,大大简化了错误报告。您可以在 GitHub 上找到它们的代码 https://github.com/jleffler/soq/tree/master/src/libsoq 。)
关于C 遍历目录并成对打印条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42263554/
我有 X 个值通过 CSV 传递到表中 - 因此我将 99315,99316,99223 并将它们拆分到单列临时表中 - CSV 中的每个值都放入一行。 我需要做的是获得成对的值的每个排列 -所以 -
我已经看到了几个关于如何生成数组中元素的所有可能组合的类似问题。但是我很难弄清楚如何编写一个只输出对组合的算法。任何建议将不胜感激! 从以下数组开始(有 N 个元素): var array = ["a
基本上我想做这样的查询: SELECT * FROM `table` WHERE (`c1`,`c2`) in (('a','b'),('c','d')) 我尝试了一些方法,但找不到任何关于如何实现的
我声明了一个 vector 对: vector > args; 然后我想像这样将一对插入 vector : args.push_back((1,-1)); 它告诉我逗号的左边操作数没有作用。我哪里出
我正在用给定的对填充 vector 的邻接列表: vector> adj[1000]; 我正在对列表进行深度优先搜索,但遇到了一些奇怪的行为。第一个 print 语句打印一些值,这意味着我在 adj[
这个问题Mysql Select some random rows and plus one specific row非常接近我想要的,只是我不知道如何将 ORDER BY 应用于我的情况。 当有人接
我在字符串向量中有大量文本(大约 700.000 个字符串)。我正在尝试替换语料库中的特定单词/短语。也就是说,我有一个包含 app 40.000 个短语的向量和一个相应的替换向量。 我正在寻找解决问
我正在寻找一种方法来找到第 2 组(对)的集群。 有没有一种简单的方法可以做到这一点? 想象一下,我有某种数据想要匹配 x和 y , 喜欢 library(cluster) set.seed(1) d
我必须正确编码 5 个 XML 保留字符(& "和 '),如下所示: "&", "&" "", ">" "\"", """ "\'", "'" 我可以一一做,
我正在处理我的作品集。我在正文中有图像我希望图像应该只使用 css/css3/Jquery 对 Angular 线而不是通过 photoshop 编辑我已经尝试对正文使用变换旋转。 但我没有得到结果。
我带着类似的问题又回来了。是否有可以返回其特定合作伙伴的数据类型?例如: ExampleType test = new ExampleType(); test.put("hello","hi"); 如
我正在对我的数据运行事后 Tukey HSD,该数据有 10 个因子级别。该表很大,我希望在成对的表中向读者展示 p 值,将 45 行表留给附录。 这是一个示例数据集: set.seed(42) x
我正在对我的数据运行事后 Tukey HSD,该数据有 10 个因子级别。该表很大,我希望在成对的表中向读者展示 p 值,将 45 行表留给附录。 这是一个示例数据集: set.seed(42) x
我想根据 std::pair 的 std::vector 找到 std::lower_bound second 元素与 lambda。 std::vector > vec; vec.resize(5
得到这个简单的python代码,和re.compile实例一样匹配。我注意到,即使我使用的是完全相同的值,它也会创建两个实例,并相应地重复它们。 我想知道是否有人可以说出这种行为的原因, 为什么要创建
首先,我是 C++ 的新手,所以我可能不得不深入研究伪代码和/或 Python 来解释我正在尝试做的事情... 我正在尝试为动画的每一帧存储多个 Sprite 的 X 和 Y 坐标对。我设想这类似于以
test1和test2在内存中的布局是否相同? std::vector > test1; std::vector test2; 其中 mystruct 定义为: struct mystru
我有这个代码: std::vector> vec; vec.emplace_back("a", 1); //success vec.emplace(vec.end(), "b", 2); //comp
跟进 this question ,我正在尝试制作箱线图和成对比较以再次显示显着性水平(仅针对重要的成对),但这次我有超过 2 个组要比较和更复杂的方面。 我将在此处使用 iris 数据集进行说明。检
我有一个 vector 对,如下所示。第一对值未排序,第二对值已排序(从零开始)。我可能想通过实现 std::vector 和 std::pair 来存储数据。当我有第一对值(未排序)时,找到相应的第
我是一名优秀的程序员,十分优秀!