在 Windows 中使用 C 列出文件夹中的文件 [英] listing files in a folder using C in Windows

查看:30
本文介绍了在 Windows 中使用 C 列出文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出这个文件夹C:\home\WORK\Desktop\Communication"中的文件.这个文件夹中有十个文件.我的代码没有错误,但没有打印任何内容.我的错误是什么?

I want to list the files in this folder "C:\home\WORK\Desktop\Communication". There are ten files in this folder. My code has no error but it's not printing anything. What is my mistake ?

#include<stdio.h>
#include<Windows.h>
#include<string.h>

int main(int argc,char *argv[])
{
  char path[]="C:\\home\\WORK\\Desktop\\Communication";
  strcat_s(path,sizeof(path),"\\*");

  WIN32_FIND_DATA fdata;
  HANDLE hFind =INVALID_HANDLE_VALUE;
  int numberOfFiles=0;
  char *files[10];

 hFind = FindFirstFile(path,&fdata);

 while((FindNextFile(hFind,&fdata))!=0)
   {
     files[numberOfFiles]=fdata.cFileName;
     numberOfFiles++;
     printf("%s\n",files[numberOfFiles]);

   }

   FindClose(hFind);

  return 0;
}

推荐答案

您的代码存在一些问题.

There are a few things wrong with your code.

  1. strcat_s 无法将 "\\*" 附加到您的 path 字符数组.缓冲区只有足够的空间来存储字符串文字.
  2. 让您声明一个缓冲区 files 的内存仅够容纳所有文件名,这让我感到不舒服.如果再添加一个文件会怎样?然后缓冲区溢出.但是,在这种情况下,它在技术上仍然有效.
  3. 这一行 printf("%s\n",files[numberOfFiles]); 是未定义的行为.您将 numberOfFiles 增加到数组中尚未初始化的位置,因此它不会打印文件名.
  4. 当您调用 FindClose 时,您会使存储在 files 中的所有指针无效.您不能再使用它们.您需要将字符串复制到新缓冲区.
  1. strcat_s can't append "\\*" to your path char array. The buffer only has enough room for storing the string literal.
  2. I'm uncomfortable with letting you declare a buffer files that has only enough memory to fit all the file names. What happens if you add one more file? Then the buffer is overrun. However, it will still technically work in this scenario.
  3. This line printf("%s\n",files[numberOfFiles]); is undefined behavior. You incremented numberOfFiles to a location in the array that has not been initialized, so it's not going to print the file name.
  4. When you call FindClose, you invalidate all of those pointers that you stored in files. You can no longer use them. You need to copy the string to a new buffer.

<小时>

以下代码有效.


The following code works.

#include<stdio.h>
#include<Windows.h>
#include<string.h>

int main(int argc,char *argv[])
{
    char path[] = "C:\\home\\WORK\\Desktop\\Communication\\*.*";
    //strcat_s(path,sizeof(path),"\\*");

    WIN32_FIND_DATA fdata;
    HANDLE hFind =INVALID_HANDLE_VALUE;
    int numberOfFiles=0;
    char* files[10]; /* you may want to expand this buffer */

    hFind = FindFirstFile(path,&fdata);

    while((FindNextFile(hFind,&fdata))!=0)
    {
        size_t len = strlen(fdata.cFileName);
        files[numberOfFiles] = malloc(len + 1 * sizeof*files); // len + 1 for null-terminator
        strcpy_s(files[numberOfFiles], len, fdata.cFileName);

        printf("%s\n",files[numberOfFiles]);
        numberOfFiles++; /* increment this AFTER you print files[numberOfFiles] */
    }

    FindClose(hFind);

    for(int i = 0; i < (sizeof(file)/sizeof(*file)); ++i) {
        free(file[i]);
    }

    return 0;
}

这篇关于在 Windows 中使用 C 列出文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆