æ åCæ¯æ²¡æç®å½ç¸å
³çå½æ°ç
CFreeæ¯16ä½çå§,é£å°±æ´ä¸ç¨æ³äº.è²ä¼¼åªè½å
åµæ±ç¼ä½¿ç¨dosä¸ææ¥å®æ.
è¿æ¯æ¢ç¼è¯å¨å§devcpp codeblock vc8 ä¹ç±»çé½å¾å¥½
ãcmailã:
è¿ä¸ªè¦ç¨å°windows API
HANDLE FindFirstFile(
LPCTSTR lpFileName,
LPWIN32_FIND_DATA lpFindFileData
);
BOOL FindNextFile(
HANDLE hFindFile,
LPWIN32_FIND_DATA lpFindFileData
);
WIN32_FIND_DATA
ãCHROXã:
å¨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);
}
ãGeomaticã:
åæ¥FindFirstFileåFindNextFileæ¯WINDOWSçAPI ææ æ以为æ¯C++çå½æ°åºéçä¸è¥¿å¢
ææç½äº ææ¾å°ç代ç åCHROXçå·®ä¸å¤
#include <stdio.h>
#include <windows.h>
BOOL IsRoot(LPCTSTR lpszPath)
{
TCHAR szRoot[4];
wsprintf(szRoot, "%c:\\", lpszPath[0]);
return (lstrcmp(szRoot, lpszPath) == 0);
}
void FindInAll(::LPCTSTR lpszPath)
{TCHAR szFind[MAX_PATH];<br>lstrcpy(szFind, lpszPath);<br>if (!IsRoot(szFind))<br>lstrcat(szFind, "\\");<br>lstrcat(szFind, "*.*"); // æ¾æææ件<br>WIN32_FIND_DATA wfd;<br>HANDLE hFind = FindFirstFile(szFind, &wfd);<br>if (hFind == INVALID_HANDLE_VALUE) // å¦æ没ææ¾å°ææ¥æ¾å¤±è´¥<br>return;<br><br>do<br>{<br>if (wfd.cFileName[0] == '.')<br>continue; // è¿æ»¤è¿ä¸¤ä¸ªç®å½<br>if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)<br>{<br>TCHAR szFile[MAX_PATH];<br>if (IsRoot(lpszPath))<br>wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);<br>else<br>wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);<br>FindInAll(szFile); // å¦ææ¾å°çæ¯ç®å½ï¼åè¿å
¥æ¤ç®å½è¿è¡éå½<br>}
else
{
TCHAR szFile[MAX_PATH];
if (IsRoot(lpszPath))
wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
else
wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);
printf("%s\n",szFile);
// 对æ件è¿è¡æä½
}
} while (FindNextFile(hFind, &wfd));
FindClose(hFind); // å
³éæ¥æ¾å¥æ
}
int main(int argc, char* argv[])
{
FindInAll("g://music");
return 0;
}
谢谢大家ç帮å©
ãGeomaticã:
è¿ä¸ªæä¹ç»ä½ 们ç»åå
é£ä¸ªåæç¨å
åç³è¯·ç 好å¤åè½ä¸ä¼ç¨å
ãcmailã:
ç¹ä¸é¢çâ管çâã
ãCHROXã:
åå¯æ¯ä¸ªå¥½ä¸è¥¿ãåµåµï¼ä½ 以åå°±æç½äºã
ãjixingzhongã:
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;
}
}
}
ãjixingzhongã:
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;
}
ãjixingzhongã:
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);
}
温馨提示:答案为网友推荐,仅供参考