Как проигнорировать скрытые файлы с opendir и readdir в библиотеке C

Списки в Kotlin имеют нулевую индексацию, поэтому для списка размером N первый элемент имеет индекс 0, а последний - индекс N-1.

В вашем цикле здесь ...

for (i in 0..response.body().size)

Вы используете инклюзивный диапазон с синтаксисом x..y, поэтому i будет работать от 0 до размера списка, что не является допустимым индексом. [1116 ]

Возможное исправление - использовать функцию until для создания диапазона, который не включает его верхнюю границу:

for (i in 0 until response.body().size)

Или вы также можете использовать indices , чтобы получить диапазон допустимых индексов для списка:

for (i in response.body().indices)

Подробнее о диапазонах см. В официальной документации .

5
задан Zifre 10 May 2009 в 16:02
поделиться

4 ответа

This is normal. If you do ls -a (which shows all files, ls -A will show all files except for . and ..), you will see the same output.

. is a link referring to the directory it is in: foo/bar/. is the same thing is foo/bar.

.. is a link referring to the parent directory of the directory it is in: foo/bar/.. is the same thing as foo.

Any other files beginning with . are hidden files (by convention, it is not really enforced by anything; this is different from Windows, where there is a real, official hidden attribute). Files ending with ~ are probably backup files created by your text editor (again, this is convention, these really could be anything).

If you don't want to show these types of files, you have to explicitly check for them and ignore them.

16
ответ дан 18 December 2019 в 06:12
поделиться

Stick a if (cur->d_name[0] != '.') before you process the name.

The UNIX hidden file standard is the leading dot, which . and .. also match.

The trailing ~ is the standard for backup files. It's a little more work to ignore those, but a multi-gigahertz CPU can manage it. Use something like if (cur->d_name[strlen(cur->d_name)-1] == '~')

2
ответ дан 18 December 2019 в 06:12
поделиться

Это поведение в точности аналогично тому, что делает ls -a . Если вам нужна фильтрация, вам нужно будет сделать это постфактум.

1
ответ дан 18 December 2019 в 06:12
поделиться

Eliminating hidden files:

DIR* pd = opendir(xxxx);
struct dirent *cur;
while (cur = readdir(pd)) {
    if (cur->d_name[0] != '.') {
        puts(cur->d_name);
    }
}

Eliminating hidden files and files ending in "~":

DIR* pd = opendir(xxxx);
struct dirent *cur;
while (cur = readdir(pd)) {
    if (cur->d_name[0] != '.' && cur->d_name[strlen(cur->d_name)-1] != '~') {
        puts(cur->d_name);
    }
}
7
ответ дан 18 December 2019 в 06:12
поделиться
Другие вопросы по тегам:

Похожие вопросы: