gpt4 book ai didi

c++ - 如何按修改日期列出目录中的文件?

转载 作者:行者123 更新时间:2023-11-28 06:06:49 27 4
gpt4 key购买 nike

目前我正在使用 readdir 并且它工作正常。现在我的文件夹变得杂乱无章,按字母顺序搜索列表(虽然不能保证,这是目录顺序)可能会令人沮丧。那么,如何修改下面的代码以按修改日期而不是当前顺序排序?

static cell AMX_NATIVE_CALL n_dir_list( AMX* amx, cell* params)
{
DIR *dir = (DIR*)params[1];

struct dirent *ent;

if ((ent = readdir (dir)) != NULL)
{
cell *buf, *addr;

amx_GetAddr(amx, params[3], &addr);

switch (ent->d_type)
{
case DT_REG:
*addr = 2;
break;
case DT_DIR:
*addr = 1;
break;
default:
*addr = 0;
}

amx_GetAddr(amx, params[2], &buf);

amx_SetString(buf, ent->d_name, 0, 0, params[4]);
return true;
}
return false;
}

readdir函数来自dirent头文件,如下:

static struct dirent *readdir(DIR *dirp)
{
DWORD attr;
if (dirp == NULL) {
/* directory stream did not open */
DIRENT_SET_ERRNO (EBADF);
return NULL;
}

/* get next directory entry */
if (dirp->cached != 0) {
/* a valid directory entry already in memory */
dirp->cached = 0;
} else {
/* get the next directory entry from stream */
if (dirp->search_handle == INVALID_HANDLE_VALUE) {
return NULL;
}
if (FindNextFileA (dirp->search_handle, &dirp->find_data) == FALSE) {
/* the very last entry has been processed or an error occured */
FindClose (dirp->search_handle);
dirp->search_handle = INVALID_HANDLE_VALUE;
return NULL;
}
}

/* copy as a multibyte character string */
DIRENT_STRNCPY ( dirp->curentry.d_name,
dirp->find_data.cFileName,
sizeof(dirp->curentry.d_name) );
dirp->curentry.d_name[MAX_PATH] = '\0';

/* compute the length of name */
dirp->curentry.d_namlen = strlen (dirp->curentry.d_name);

/* determine file type */
attr = dirp->find_data.dwFileAttributes;
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
dirp->curentry.d_type = DT_CHR;
} else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
dirp->curentry.d_type = DT_DIR;
} else {
dirp->curentry.d_type = DT_REG;
}
return &dirp->curentry;
}

最佳答案

FindNextFile 的 Microsoft 文档说,“如果必须对数据进行排序,应用程序必须在获得所有结果后进行排序。”因此,如果您希望目录按修改日期排序,您必须将其全部读入并自行排序。

关于c++ - 如何按修改日期列出目录中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32213767/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com