如何读取C中的文件目录以获取所有非文件夹文件的名称
我试图读取给定的目录文件路径,并将所有非文件夹文件的名称放入字符串数组中因此,我需要解决的问题是如何明确地不获取文件夹类型的文件,而是获取所有其他文件类型的名称,并将它们存储到字符串数组中。稍后我计划使用线程来读取这些单独的文件,但我需要能够正确地存储文件名。下面是我当前使用的代码。还应该注意,这段代码是由fork()命令的子进程执行的,但我不确定这是否与问题有关。任何帮助都将不胜感激。谢谢 例如: 在Home/Documents中有4个文件:hello.txt something.dat folder1 something 2.dat 我的字符串数组的值应该是hello.txt、something.dat和something2.dat 注意:对于我来说,在我浏览目录时不做这些文件是可以的,因为文件本身在所有内容方面都不会被更改如何读取C中的文件目录以获取所有非文件夹文件的名称,c,directory,process,C,Directory,Process,我试图读取给定的目录文件路径,并将所有非文件夹文件的名称放入字符串数组中因此,我需要解决的问题是如何明确地不获取文件夹类型的文件,而是获取所有其他文件类型的名称,并将它们存储到字符串数组中。稍后我计划使用线程来读取这些单独的文件,但我需要能够正确地存储文件名。下面是我当前使用的代码。还应该注意,这段代码是由fork()命令的子进程执行的,但我不确定这是否与问题有关。任何帮助都将不胜感激。谢谢 例如: 在Home/Documents中有4个文件:hello.txt something.dat fo
//char* directory is an absolute filePath to the directory
void getFilesFromDirectory(char* directory, pid_t process)
{
int index =0;
DIR *dir;
struct dirent *ent;
//Can hold only 500 valid files in the folder
char *stringArray[500];
if ((dir = opendir (directory)) != NULL)
{
while ((ent = readdir (dir)) != NULL)
{
strcpy(stringArray[index],ent->d_name);
index++;
}
closedir (dir);
}
else
{
/* could not open directory */
perror ("");
}
//Everything Below is not related to the problem. Just what I am using it for.
pthread_t threadArray[index];
pthread_t senderThread;
//thread_param_t parameterSender;
thread_param_t paramterArray[index];
sem_init(&empty,0,bufferSize);
sem_init(&full, 0, 0);
sem_init(&mutex, 0, 1);
//parameterSender.listItem = listHead;
pthread_create(&senderThread, NULL, senderFunction, NULL);
//int threadCounter = 0;
for(int i =0; i<index; i++)
{
paramterArray[i].fileLocation = strcat(directory, stringArray[i]);
pthread_create(&threadArray[i], NULL, threadFunction, paramterArray + i);
}
}
//char*目录是指向该目录的绝对文件路径
void getFilesFromDirectory(char*目录,pid\u t进程)
{
int指数=0;
DIR*DIR;
结构导向;
//文件夹中只能保存500个有效文件
char*stringArray[500];
if((dir=opendir(directory))!=NULL)
{
while((ent=readdir(dir))!=NULL)
{
strcpy(stringArray[index],ent->d_name);
索引++;
}
closedir(dir);
}
其他的
{
/*无法打开目录*/
佩罗尔(“”);
}
//下面的所有内容都与问题无关。只是我使用它的目的。
pthread_t threadArray[index];
pthread\u t senderThread;
//线程参数发送器;
线程参数[索引];
sem_init(&empty,0,bufferSize);
sem_init(&full,0,0);
sem_init(&mutex,0,1);
//参数sender.listItem=listHead;
pthread_create(&senderThread,NULL,senderFunction,NULL);
//int-threadCounter=0;
对于(int i=0;i您可以检查stringArray
中没有“.”的文件,并将此指针设置为Null。(缺少文件)
更好的选择是:
#include <stdio.h>
#include <dirent.h>
int main()
{
DIR *folder;
struct dirent *entry;
int files = 0;
folder = opendir(".");
if(folder == NULL)
{
perror("Unable to read directory");
return(1);
}
printf("debug\n");
while( (entry=readdir(folder)) )
{
files++;
printf("File %3d: %s :: %s\n",
files,
entry->d_name,
(entry->d_type == DT_DIR)?"Directory" : "File"
);
}
closedir(folder);
return(0);
}
#包括
#包括
int main()
{
目录*文件夹;
结构方向*条目;
int文件=0;
folder=opendir(“.”);
如果(文件夹==NULL)
{
perror(“无法读取目录”);
申报表(1);
}
printf(“debug\n”);
而((entry=readdir(folder)))
{
文件++;
printf(“文件%3d:%s::%s\n”,
文件夹,
输入->d_名称,
(条目->d_类型==DT_目录)?“目录”:“文件”
);
}
closedir(文件夹);
返回(0);
}
“我不确定这是否与问题有关”。什么问题?您实际上没有提出问题或描述您需要帮助解决的问题。char*stringArray[500];strcpy(stringArray[index],ent->d_name)
您正在复制到未初始化的指针。您需要为每个字符串分配内存。当文件超过500个时,请务必检查index
的值。提供一个和一个特定的问题。否则此Q应该关闭。您真的需要全部吗?通常,最好读取一个名称和n使用它,然后转到下一个。如果将所有数据读入内存,然后在内存中的副本上迭代,则很可能使用了过时的数据。文件名可能没有点,例如可执行文件;为什么要在stringArray[]中设置指针
到NULL
当遇到文件夹时,您可以直接进入下一个条目?这是真的。但是您有更好的工作区吗?这取决于您需要什么文件。但我假设这是关于“正常”的文件,例如.txt、.md.mp3等。此外:Linux中也有至少带有点的文件夹。但它的名称为[0]位置“正常”文件名中并不总是有点;这在*nix系统上尤其常见。也不仅仅是可执行文件。查看任何开源linux项目的安装目录,您可能会看到名为INSTALL
,README
,bug
,等等的文本文件。真正的答案取决于操作系统,但请参见他提出了我在上面的评论中链接到的副本作为替代方案