c查找文件夹
读取的代码方式如下:
intmain()
{
longfile;
struct_finddata_tfind;
_chdir("d:\");
if((file=_findfirst("*.*",&find))==-1L)
{
printf("空白! ");
exit(0);
}
printf("%s ",find.name);
while(_findnext(file,&find)==0)
{
printf("%s ",find.name);
}
_findclose(file);
return0;
}
② c++中如何搜索一个目录下面所有文件夹名,
据我了解C++中没有这样的函数,但是你可以自己写一个这样的函数
1. 首先判断你的参数是一个文件还是一个路径
2. 如果是一个文件,直接递归的查找,并且返回路径
3. 如果是目录,首先递归的查找文件夹,找到文件夹之后判断文件夹中是否有这样的文件,如果有返回找到的路径,没有就返回false
③ c语言 怎么查找 当前目录有哪些 文件
在命令提示符窗口运行:findfile (盘符): *.(文件后缀)
如:[sourcecode language=”plain”]findfile d: *.txt [/sourcecode]
即为找出d盘根目录下的所有.txt后缀的文件并写入文件路径于文件中。
④ c语言实验:查找c盘中的所有文件夹,对于子文件夹中的文件也要进行查找输出。
优化了一下,但还是有bug,需要细查
#include<io.h>
#include<stdio.h>
#include<string.h>
#define_A_SUBDIR0x10/*Subdirectory*/
voidsearch(char*str1)
{
longflag1=0;
longhandle1;
chars1[]="\",s2[]="*.*",s3[100],name[100];
struct_finddata_tffblk1;
strcat(str1,s1);strcpy(s3,str1);strcat(str1,s2);
handle1=_findfirst(str1,&ffblk1);
while(!flag1)
{
if(strcmp(ffblk1.name,".")&&strcmp(ffblk1.name,".."))
{
if(ffblk1.attrib==_A_SUBDIR){
printf("%sisasub-directory ",ffblk1.name);
strcpy(name,ffblk1.name);
strcat(s3,name);
search(s3);
}
else{
printf("%s ",ffblk1.name);
}
}
flag1=_findnext(handle1,&ffblk1);
}
}
voidmain()
{
// struct_finddata_tffblk;
charstr[200]="C:";//必须是数组,不然空间不够
search(str);
}
⑤ c++语言编程:怎样全盘搜索一个已知文件名的文件夹,并得到该文件夹的路径。
void CFileScanRollDlg::BrowsDir(CString strDir)
{
CFileFind files;
CString szDir=strDir;
if(szDir.Right(1) != "\\")
szDir+="\\";
szDir+="*.*";
BOOL res=files.FindFile(szDir);//查找目录
while(res)
{
Doevents();
res=files.FindNextFile();
if(files.IsDirectory() && !files.IsDots())//如果是一个子目录,用递归继续往深一层找
{
//这里判断是不是你要的目录
BrowsDir(files.GetFilePath());
}
else if(!files.IsDirectory() && !files.IsDots())
{
CString str=strDir;
str=str+"\\"+files.GetFileName();
//在这里判断是不是你要的文件名称
Sleep(20);
}
}
files.Close();
}
void CFileScanRollDlg::Doevents()//开辟线程避免假死状态
{
MSG msg;
if (PeekMessage(&msg, NULL, 0,0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
⑥ 如何用C语言获取目录下的文件和目录列表
1、可以利用getenv函数来实现。
在Linux系统中,home目录的定义是通过系统环境变量中的HOME变量值来确定的,在shell下可以通过
echo $HOME来查看。
而在C语言中,库函数getenv可以用作获取环境变量值。该函数位于stdlib.h, 原型为
char *getenv(char *name);
功能为获取名字为name的环境变量字符串。
所以,下面代码就可以获取到home目录名了:
2、例程:
char *home;
home = getenv("HOME");
printf("the home path is %s\n", home);
⑦ 请我如何c语言遍历文件夹
#include <windows.h>
/************************************************/
*参数说明:
char *pszDestPath为需要遍历的目标路径
/************************************************/
EnmuDirectory(char *pszDestPath)
{
//此结构说明参MSDN
WIN32_FIND_DATA FindFileData;
//查找文件的句柄
HANDLE hListFile;
//绝对路径,例:c:\windows\system32\cmd.exe
char szFullPath[MAX_PATH];
//相对路径
char szFilePath[MAX_PATH];
//构造相对路径
wsprintf(szFilePath, "%s\\*", pszDestPath);
//查找第一个文件,获得查找句柄,如果FindFirstFile返回INVALID_HANDLE_VALUE则返回
if((hListFile = FindFirstFile(szFilePath, &FindFileData)) == INVALID_HANDLE_VALUE)
{
//查找文件错误
return 1;
}
else
{
do
{
//过滤.和..
//“.”代表本级目录“..”代表父级目录
if( lstrcmp(FindFileData.cFileName, TEXT(".")) == 0 ||
lstrcmp(FindFileData.cFileName, TEXT("..")) == 0 )
{
continue;
}
//构造全路径
wsprintf(szFullPath, "%s\\%s", pszDestPath, FindFileData.cFileName);
//读取文件属性,如果不是文件夹
if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
//这里你可以自己添加分析是某种类型文件的代码。可以根据
//扩展名分析。
//这里有个实例,你可以看看
//有必要初始化一下
char *pszFileType = NULL;
//把pszFileType指向cFileName的倒数第三个数符。因为一般扩展名长为3个字符。
//当然,你也可以用其它方法分析扩展名。或倒序查“.”
pszFileType = &(FindFileData.cFileName[strlen(FindFileData.cFileName) - 3]);
//如果是jpg结尾的文件
if(!stricmp(pszFileType, "jpg"))
{
FILE *fp;
//或许这里打开C:\\data.txt不应该用"w+",你可试着来
fp = fopen("c:\\data.txt", "w+");
if(fp) fputs(szFullPath, fp);
fclose(fp);
}
}
//如果是文件夹,则递归调用EnmuDirectory函数
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
EnmuDirectory(szFullPath);
}
//循环,查找下一个文件
}while(FindNextFile(hListFile, &FindFileData));
}
//关闭句柄
FindClose(hListFile);
//清空结构。可有可无的一句代码。函数退出会自动清空。
ZeroMemory(&FindFileData, sizeof(FindFileData));
return 0;
}
这是windows api版的,还有MFC版的和C版的。要的话来找我Q:503267714
⑧ VC环境中用C语言查找当前路径下的所有文件和文件夹的函数是什么
这是我的TFTP程序中的一个函数,是搜索当前盘符下的所有文件,包括文件的大小,并发送到客户端,其中就有查找当前路径下的文件,你自己挑一下,应该能完成你的需求。
void FileList(sockaddr_in sour_addr,char strStartDir[])
{
char sendbuffer[1024];
sockaddr_in destaddr;
int sourlen = 0;
int ret = 0;
int len = 0;
int flen = 0;
fd_set fdr;
unsigned short blocknum = 0;
FILE *file;
char filename[128];
strcpy(filename,strStartDir+2); /*获取文件名*/
strcat(filename,"\\*");
destaddr.sin_family = AF_INET;
destaddr.sin_port = sour_addr.sin_port;
destaddr.sin_addr.s_addr = inet_addr(desthost);//
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(filename, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle");
}
else
{
while(FindNextFile(hFind,&FindFileData))
{
printf(FindFileData.cFileName);
printf("\r\n");
memset(sendbuffer,'\0',1024);
len = filldata(blocknum++,FindFileData.cFileName,strlen(FindFileData.cFileName),sendbuffer,sizeof(sendbuffer));
ret = sendto(serverSock,sendbuffer,len,0,(sockaddr *)&destaddr,sizeof(destaddr));
}
len = fillover(blocknum,"Over",4,sendbuffer,sizeof(sendbuffer));
ret = sendto(serverSock,sendbuffer,len,0,(sockaddr *)&destaddr,sizeof(destaddr));
FindClose(hFind);
return;
}
}