简体   繁体   English

如何在C中不使用“ dirent.h”(Visual Studio 2017 Windows)的情况下查找特定目录中的所有文件名

[英]How to find all the file names in a specific directory without using “dirent.h” (visual studio 2017 windows) in C

I have a mid-semester project, to make an anti-virus, that get as main arguments a directory and a path: The path is to a file that contains a virus signature. 我有一个学期中期的项目,要制作一个防病毒软件,它的主要参数是目录和路径:路径是指向包含病毒签名的文件。 The anti-virus's goal is to scan all the files in the given directory for the virus signature. 防病毒软件的目标是扫描给定目录中的所有文件以获取病毒签名。 I've finished the whole project, except the part that finds all the files in a specific given directory. 我已经完成了整个项目,除了可以在特定给定目录中查找所有文件的部分。 Since this project is checked by an auto-feedback system that doesn't have any external libraries and operates on an unknown operating system (even my instructor don't know, it's the course's system), it has to be a cross-platform solution that doesn't rely on any libraries but stdio.h, stdlib.h, string.h and windows.h 由于该项目是由没有任何外部库并且在未知操作系统运行的自动反馈系统检查的(即使我的讲师也不知道,它是课程的系统),因此它必须是跨平台的解决方案除了stdio.h,stdlib.h,string.h和windows.h,它不依赖任何库

Almost every tutorial and example of doing this is with HANDLE (which we haven't learned yet, and not supposed to use) or with . 几乎每个教程和示例都使用HANDLE(我们尚未学习,并且不应该使用)或。 The problem is to find a way of doing it without dirent library. 问题是找到一种无需Dirent库的方法。

I need some function that gets all the files in a directory 我需要一些功能来获取目录中的所有文件

Something like this: 像这样:

char** fileNames = somefunc(char* dir)

There is no way to do this with the standard library alone; 仅凭标准库是无法做到这一点的。 standard C has no knowledge of directories. 标准C不了解目录。 Every OS has a way of doing this, though, so you'll want to find the OS family for your checker (Windows, Linux, etc.) and look up how to do it for that OS. 但是,每个操作系统都有执行此操作的方法,因此,您需要查找检查程序的操作系统系列(Windows,Linux等),并查找如何针对该操作系统执行此操作。

Libraries built into the OS must be available, since they come with the system. 操作系统内置的库必须可用,因为它们是系统附带的。 You cannot run any program at all without them. 没有它们,您将无法运行任何程序 (This is technically not true, but it's accurate enough to cover all interesting cases.) (从技术上讲,这是不正确的,但它足以涵盖所有有趣的情况。)

EDIT: if your checker runs under Windows (judging from your comments), you'll want to use FindFirstFile and FindNextFile . 编辑:如果您的检查器在Windows下运行(从您的评论来看),则需要使用FindFirstFileFindNextFile They do rely on handles, though (like virtually all of the Windows API), so you'll have to look into that. 但是,它们确实依赖于句柄(就像几乎所有Windows API一样),因此您必须对此进行研究。 (A handle is just an opaque pointer with special meaning to the Windows kernel. You can treat it pretty much like a void * . Don't get too fixated on the data type.) (句柄只是对Windows内核具有特殊含义的不透明指针。您可以将其视为void *对待。不要太在意数据类型。)

EDIT 2: if you're looking for something to get you started: 编辑2:如果您正在寻找入门的方法:

char ** list_directory (const char * directory) {
  char * search_path = malloc(strlen(directory) + 3);
  strcpy(search_path, directory);
  strcat(search_path, "\\*");
  WIN32_FIND_DATA fd;
  SetLastError(0);
  HANDLE hsrch = FindFirstFile(search_path, &fd);
  free(search_path);
  if (hsrch == INVALID_HANDLE_VALUE) {
    if (GetLastError() != ERROR_FILE_NOT_FOUND) return NULL;
    SetLastError(0);
    return calloc(1, sizeof(char *)); // no files
  }
  char ** result = malloc(sizeof(char *));
  unsigned count = 0;
  while (!GetLastError()) {
    result[count] = malloc(strlen(fd.cFileName) + 1);
    strcpy(result[count ++], fd.cFileName);
    FindNextFile(hsrch, &fd);
    result = realloc(result, (count + 1) * sizeof(char *));
  }
  if (GetLastError() == ERROR_NO_MORE_FILES)
    SetLastError(0);
  FindClose(hsrch);
  result[count] = NULL;
  return result;
}

The dirent.h header is a POSIX implementation of traversing directories, meaning it runs only on UNIX-like systems. dirent.h标头是遍历目录的POSIX实现,这意味着它仅在类似UNIX的系统上运行。 For Windows you should use the _findfirst and _findnext functions as specified here . 对于Windows,应使用此处指定的_findfirst_findnext函数。 Unlike FindFirstFile and FindNextFile these do not use a HANDLE . FindFirstFileFindNextFile不同,它们不使用HANDLE

For example: 例如:

intptr_t ffhandle;
struct _finddata_t ffinfo;

if ((ffhandle = _findfirst("c:\\path\\to\\dir\\*.*", &ffinfo)) == -1) {
    perror("findfirst failed");
    exit(1);
}
do {
    printf("found file %s\n", ffinfo.name);
} while (_findnext(ffhandle, &ffinfo) == 0);
_findclose(ffhandle);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM