首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

怎么用C实现遍历文件夹

2012-04-14 
如何用C实现遍历文件夹?我是个C语言的初学者才开始写东西最近准备写个批量文件更新工具想遍历文件夹在网上

如何用C实现遍历文件夹?
我是个C语言的初学者       才开始写东西
最近准备写个批量文件更新工具
想遍历文件夹       在网上也找到很多代码
但是没有C的       C++的用CFree单独编译可以运行
但是和C的代码一起编译     就出错
看过C++的代码     是用FindFirstFile和FindNextFile两个函数进行递归
完成功能的     但是在C的函数库里没有找到这类搜索文件的函数
希望高手能指点下
不胜感激

[解决办法]
标准C是没有目录相关的函数的
CFree是16位的吧,那就更不用想了.貌似只能内嵌汇编使用dos中断来完成.

还是换编译器吧devcpp codeblock vc8 之类的都很好
[解决办法]
这个要用到windows API

HANDLE FindFirstFile(
LPCTSTR lpFileName,
LPWIN32_FIND_DATA lpFindFileData
);

BOOL FindNextFile(
HANDLE hFindFile,
LPWIN32_FIND_DATA lpFindFileData
);

WIN32_FIND_DATA
[解决办法]
在Win32平台下不用windows api,有好多功能实现起来都很费劲,特别是系统相关的
再说用api又不是什么丢人的事。开发可移植程序除外。
用FindFirstFile和FindNextFile两个api很容易实现
////////////////////////////////////////////////
void FindFile(LPCTSTR lpszPath) {
TCHAR szFind[MAX_PATH];
lstrcpy(szFind,lpszPath);

if(!IsRoot(szFind)) lstrcat(szFind, "\\ ");
lstrcat(szFind, "*.* ");
WIN32_FIND_DATA wfd;
HANDLE hFind=FindFirstFile(szFind,&wfd);

if(hFind==INVALID_HANDLE_VALUE) return;
do{
if(lstrcmp(wfd.cFileName, ". ")==0||lstrcmp(wfd.cFileName, ".. ")==0) continue;
char szFile[MAX_PATH];
lstrcpy(szFile,lpszPath);
if(!IsRoot(szFile)) lstrcat(szFile, "\\ ");
lstrcat(szFile,wfd.cFileName);
if((GetFileAttributes(szFile)&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY){
FindFile(szFile); //递归
}
else {
} //Do your things
}
} while (FindNextFile(hFind,&wfd));
CloseHandle(hFind);
}
[解决办法]
点上面的“管理”。
[解决办法]
分可是个好东西。呵呵,你以后就明白了。
[解决办法]
DEV C++, 利用链表实现目录内所有文件列表显示

#include <stdio.h>
#include <dirent.h>
/*#include <alloc.h> */
#include <string.h>

void main(int argc,char *argv[])
{
DIR *directory_pointer;
struct dirent *entry;
struct FileList
{
char filename[64];
struct FileList *next;
}start,*node;
if (argc!=2)
{
printf( "Must specify a directory\n ");
exit(1);
}
if ((directory_pointer=opendir(argv[1]))==NULL)
printf( "Error opening %s\n ",argv[1]);
else
{
start.next=NULL;
node=&start;
while ((entry=readdir(directory_pointer))!=NULL)
{
node-> next=(struct FileList *)malloc(sizeof(struct FileList));
node=node-> next;
strcpy(node-> filename,entry-> d_name);
node-> next=NULL;
}
closedir(directory_pointer);
node=start.next;
while(node)
{
printf( "%s\n ",node-> filename);
node=node-> next;
}
}
}

[解决办法]
linux下面的,作者不是我

A Demo written by camelrain



/*
the program find a file from current directory or your defined directory
commond optinon [path] filename
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#define LENGTH 256

/* just if is a directory*/
static int IsDir (char * name);
/* search target file, arg1 is current path, arg2 is target file*/
static void search_file (char * path, char * name);

static int search_flag=0;


/*just if is a directory*/
static int IsDir (char * name) {
struct stat buff;

if (lstat(name,&buff) <0)
return 0; //if not exist name ,ignore

/*if is directory return 1 ,else return 0*/
return S_ISDIR(buff.st_mode);
}


/*search target file*/
static void search_file (char * path, char * name) {
DIR *directory;
struct dirent * dir_entry;
char buffer[LENGTH];

if ((directory=opendir(path)) == NULL) {
fprintf(stderr, "%s ", path);
perror( " ");
return;
}


while (dir_entry=readdir(directory)) {
if (!strcmp(dir_entry-> d_name, ". ")||!strcmp(dir_entry-> d_name, ".. ")) {
/* do nothing*/
}
else {
/* if is boot directory add "/ " */
if ((strcmp(path, "/ "))==0)
sprintf(buffer, "%s%s ",path,dir_entry-> d_name);
/* if is not boot directory do not add "/ "*/
else
sprintf(buffer, "%s/%s ",path,dir_entry-> d_name);

//printf( "Now file : %s\n ",buffer);
if (IsDir(buffer))
search_file (buffer , name);
else {
if (strcmp(dir_entry-> d_name,name)==0)
{
printf( "%s\n ",buffer);
search_flag=1;
}
}
}
}
closedir(directory);
}

int main(int argc, char *argv[])
{
static char * current_dir;
static char * filename;
int length;

if (argc==1) {
printf( "A few parameters!!\n ");
return 0;
}

if (argc==2) {
current_dir=(char * )getcwd(current_dir,LENGTH);
filename=argv[1];
}

if (argc==3) {
length=strlen(argv[1]);

if (length> 1 && (argv[1][length-1]== '/ ')) {
argv[1][length-1]= '\0 ';
//printf( "%d\n ",strlen(argv[1]));
}

current_dir=argv[1];
filename=argv[2];
}

search_file(current_dir,filename);

if (!search_flag)
printf( "Not found this(%s) file!\n ",filename);

return 0;
}
[解决办法]
VC 下的:

long handle;
struct _finddata_t filestruct;  
char path_search[_MAX_PATH];
handle = _findfirst( "目录 ",&filestruct);
if((handle == -1)) return;
if( ::GetFileAttributes(filestruct.name)& FILE_ATTRIBUTE_DIRECTORY )
{
if( filestruct.name[0] != '. ' )


{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir( ".. ");
}
}
else
{
if( !stricmp(filestruct.name, szFilename) )
{
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);
}
[解决办法]
强贴,留名,学习了...
[解决办法]
全是四星级的,收藏
[解决办法]
收藏了 多谢瞌睡虫
[解决办法]
都写得很清楚了

热点排行
Bad Request.