怎样用c语言获得一个文件夹中的所有文件名?

zhoulifn 2005-06-30 10:03:13
os是windows。 谢谢
...全文
669 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
juan666 2005-07-12
  • 打赏
  • 举报
回复
VC6.0 兼容TC,我运行过多次,运行不了
zhoulifn 2005-06-30
  • 打赏
  • 举报
回复
为什么系统提示:Cannot open include file: 'dir.h': No such file or directory
robinzsy 2005-06-30
  • 打赏
  • 举报
回复
函数名: findfirst, findnext
功 能: 搜索磁盘目录; 取得下一个匹配的findfirst模式的文件
用 法: int findfirst(char *pathname, struct ffblk *ffblk, int attrib);
int findnext(struct ffblk *ffblk);
程序例:

/* findnext example */

#include <stdio.h>
#include <dir.h>

int main(void)
{
struct ffblk ffblk;
int done;
printf("Directory listing of *.*\n");
done = findfirst("*.*",&ffblk,0);
while (!done)
{
printf(" %s\n", ffblk.ff_name);
done = findnext(&ffblk);
}

return 0;
}



foochow 2005-06-30
  • 打赏
  • 举报
回复
WIN32_FIND_DATA find_data;
HANDLE search_handle;
search_handle = FindFirstFile( DirectoryPath,&find_data );
if (search_handle != INVALID_HANDLE_VALUE)
{
do
{
if (find_data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
CString name = find_data.cFileName;
if( (name == ".")||(name == "..") )
continue;
}
}while(FindNextFile(search_handle,&find_data));

FindClose(search_handle);
gonch 2005-06-30
  • 打赏
  • 举报
回复
MSDN里面的例子
用FindFirstFile

#include <windows.h>
#include <stdio.h>

WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = "c:\\TEXTRO\\";
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];

BOOL fFinished = FALSE;

// Create a new directory.

if (!CreateDirectory(szDirPath, NULL))
{
printf("Couldn't create new directory.");
return;
}

// Start searching for .TXT files in the current directory.

hSearch = FindFirstFile("*.txt", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
printf("No .TXT files found.");
return;
}

// Copy each .TXT file to the new directory
// and change it to read only, if not already.

while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
printf("Couldn't copy file.");
return;
}

if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
MessageBox(hwnd, "No more .TXT files.",
"Search completed.", MB_OK);
fFinished = TRUE;
}
else
{
printf("Couldn't find next file.");
return;
}
}
}

// Close the search handle.

FindClose(hSearch);

zhoulifn 2005-06-30
  • 打赏
  • 举报
回复
foochow(恰似你的温柔) 的回答不知道对不对 但我感觉这个好像不是c吧 应该是c++的代码
jixingzhong 2005-06-30
  • 打赏
  • 举报
回复
在WIN中


foochow(恰似你的温柔) 的回答不对吗??


我很困惑!!

jixingzhong 2005-06-30
  • 打赏
  • 举报
回复
dir.h

在TC下使用

同时,TC中还有其他几个关于目录操作的函数,

可以实现一些其他的功能。
juan666 2005-06-30
  • 打赏
  • 举报
回复
robinzsy(): 我想问你:你的程序在那抄的啊! (dir.h)在windows下运行不了,我上次也遇到了这样的事,程序出不来,就是因为(dir.h)编译通不过啊~!

请楼上的尽快解答

70,023

社区成员

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

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