请问如何用c读取并显示一个目录下的所有文件名

hylz0 2005-04-24 08:34:17
各位帮帮忙,谢谢
...全文
456 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
melon_soup 2005-04-25
  • 打赏
  • 举报
回复
用FindFile函数吧。一个递归就可以实现了。
dataat 2005-04-25
  • 打赏
  • 举报
回复
呵呵,我忘了问了,你是在什么系统下面做啊?我是在红帽9下面的。在库中有这个头文件,/usr/include/dirent.h,其他系统的你就自己找找吧,应该有类似的。
stonegoldaustin 2005-04-25
  • 打赏
  • 举报
回复
#include <iostream>
#include <stdlib.h>
#include <dir.h>
using namespace std;

int main(int argc, char *argv[])
{
char* mPath = "C:\\*.*";
int mResult;
struct _finddata_t mData;

mResult = _findfirst(mPath, &mData);

if(mResult)
{
cout << mData.name << endl;
while(_findnext(mResult, &mData)==0)
{
cout << mData.name << endl;
}
}
_findclose(mResult);
system("PAUSE");
return 0;
}
galanz 2005-04-25
  • 打赏
  • 举报
回复
mark
mostideal 2005-04-24
  • 打赏
  • 举报
回复
只有DIR.H
系统不含有dirent.h头文件。。
violetkz 2005-04-24
  • 打赏
  • 举报
回复
从网上找来的,希望对你有帮助。
http://www.pcdog.com/p/html/20041125/251120042694_1.htm


#include 〈direct.h〉

  #include 〈io.h〉

  ......

  void CSearchDlg::OnButtonSearch()

  {

    // TODO: Add your control notification handler code here

    char szFilename[80];

    // 字符串 szFilename 表示要查找的文件名
    strcpy(szFilename,″Mytext.txt″);

    _chdir(″d:\\″); // 进入要查找的路径(也可为某一具体的目录)

    // 查找文件, 如果查到则显示文件的路径全名

    Search_Directory(szFilename);

    // 为CSearchDlg类的一成员函数

    MessageBox(″查找文件完毕!″);

    // 显示查找完毕的信息

  }

  3.在CSearchDlg类中增加成员函数Search_Directory,它将完成具体的文件查找工作,代码如下:

  void CSearchDlg::Search_Directory(char* szFilename)

  {

    long handle;
    struct _finddata_t filestruct;  

    //表示文件(或目录)的信息
    char path_search[_MAX_PATH];

    //表示查找到的路径结果
    // 开始查找工作, 找到当前目录下的第一个实体(文件或子目录),
    // ″*″表示查找任何的文件或子目录, filestruct为查找结果

    handle = _findfirst(″*″, &filestruct);

    // 如果handle为-1, 表示当前目录为空, 则结束查找而返回
    if((handle == -1))
      return;

    // 检查找到的第一个实体是否是一个目录(filestruct.name为其名称)
    if( ::GetFileAttributes(filestruct.name) & FILE—ATTRIBUTE—DIRECTORY )
    {
      // 如果是目录, 则进入该目录并递归调用函数Search_Dirctory进行查找,
      // 注意: 如果目录名的首字符为′.′(即为″.″或″..″), 则不用进行查找
      if( filestruct.name[0] != ′.′ )
      {
        —chdir(filestruct.name);
        Search_Directory(szFilename);
        // 查找完毕之后, 返回上一级目录
        —chdir(″..″);
      }
    }

    else // 如果第一个实体不是目录, 则检查是否是要查找的文件
    {
      // stricmp对两字符串进行小写形式的对比, 返回为0表示完全一致
      if( !stricmp(filestruct.name, szFilename) )
      {
        // 先获得当前工作目录的全路径
        —getcwd(path_search,—MAX—PATH);
        // 再获得文件的完整的路径名(包含文件的名称)
        strcat(path_search,″\\″);
        strcat(path—search,filestruct.name);
        MessageBox(path_search); //输出显示
      }
    }
    // 继续对当前目录中的下一个子目录或文件进行与上面同样的查找

    while(!(—findnext(handle,&filestruct)))
    {
      if( ::GetFileAttributes(filestruct.name) & FILE—ATTRIBUTE—DIRECTORY )
      {
        if(*filestruct.name != ′.′)
        {
          —chdir(filestruct.name);
          Search_Directory(szFilename);
          —chdir(″..″);
        }
      }
      else
      {
        if(!stricmp(filestruct.name,szFilename))
        {
          —getcwd(path—search,—MAX—PATH);
          strcat(path_search,″\\″);
          strcat(path_search,filestruct.name);
          MessageBox(path_search);
        }
      }
    }
    —findclose(handle);
    // 最后结束整个查找工作
  }
xunknown 2005-04-24
  • 打赏
  • 举报
回复
这里有一个C++的例子
你可以考虑用C来实现
http://blog.csdn.net/xunknown/archive/2005/04/09/340701.aspx
hylz0 2005-04-24
  • 打赏
  • 举报
回复
fatal error C1083: Cannot open include file: 'dirent.h': No such file or directory

怎么会这样
dataat 2005-04-24
  • 打赏
  • 举报
回复
#include <stdio.h>
#include<dirent.h>
#include<sys/types.h>

int main(int argc, char *argv[])
{
DIR *dp;
struct dirent * dirp;
{
};
if(argc<2)
{
printf("Usage :dir path\n");
exit(-1);
}

if((dp=opendir(argv[1]))==NULL)
{
printf("opendir error");
exit(-1);
}

while((dirp=readdir(dp))!=NULL)
printf("%s \t \n",dirp->d_name);

closedir(dp);
return 0;
}
上面的程序符合要求,关键是dirent结构。你看看吧。
hylz0 2005-04-24
  • 打赏
  • 举报
回复
要实现dir命令功能
llmsn 2005-04-24
  • 打赏
  • 举报
回复
看微软的DOS系统是怎么实现的.是不是用DIR显示子目录命令啊.

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧