⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ufind.cpp

📁 liu7788414
💻 CPP
字号:
// UFind.cpp : Defines the entry point for the console application.
//
#pragma warning(disable :4786)

#include "stdafx.h"
#include <stdlib.h>
#include <assert.h>
#include <windows.h>
#include <direct.h>
#include <string>
#include <iostream>
#include <vector>

using namespace std;
/* dir    : the directory to be finded
 * prefix : 
 */
static void WalkDir1(char *dir,vector<string> &names)
{
	WIN32_FIND_DATA find;
	char *oldDir;
	oldDir = _getcwd(0,0);

	if(dir){
		if(-1 == _chdir(dir)){
			printf("failed to change to dir %s\n",	dir);
			getchar();
			exit(-1);
		}
	}
	char *curDir= _getcwd(0,0);
	HANDLE findH = FindFirstFile("*.*",&find);
	assert(INVALID_HANDLE_VALUE != findH);

	while(FindNextFile(findH,&find)){
		//ignore .. (current directory)
		if(0 == strcmp(find.cFileName ,".."))
			continue;
		//recursively list subdirectory
		if(find.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY){
			WalkDir1(find.cFileName,names );
			continue;
		}
		names.push_back(string(curDir)+'\\'+find.cFileName );
	}
	FindClose(findH);
	//maintain old current directory
	if(-1 == _chdir(oldDir)){
		printf("failed to change to dir %s\n",	dir);
		getchar();
		exit(-1);
	}
	free(curDir);
	free(oldDir);
}
/* implment unix's find
 * that is, given a directory name, list all file under this directory recursively
 * Why not implment the who function in WalkDir?
 * because : WalkDir need to be called recursively, if names is a local variable, you cann't return it.
 */
vector<string>	WalkDir(char *dir)
{
	vector<string> names(0);
	WalkDir1(dir,names);	
	//the names will be copied to caller, it would be better that names are passed in by reference.
	//But I like return a vector of string. 
	return names;
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -