当前位置:我的异常网» C语言 » findfirst findnext函数解决思路
findfirst findnext函数解决思路
www.myexceptions.net 网友分享于:2015-01-10 浏览:0次
findfirst findnext函数
/* 在程序目录下查找指定的文件,若查找找则打印出文件名 */
/* findfirst findnext函数应用程序 */
VC windows Linux gcc都有可能用到
问题:编译时找不到findfirst findnext函数,dir.h里面没有findfirst findnext函数
/* findnext example */
#include
#include
//两个函数需要定义一个结构体来存储函数返回的数据。结构体如下:
struct ffblk
{
char ff_reserved[21]; /*DOS保留字*/
char ff_attrib; /*文件属性*/
int ff_ftime; /*文件时间*/
int ff_fdate; /*文件日期*/
long ff_fsize; /*文件长度*/
char ff_name[13]; /*文件名*/
}
//将结构体中的ff_name[13]显示出来即可。
void 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);
}
}
------解决思路----------------------
_findfirst, _findfirsti64, _wfindfirst, _wfindfirsti64
Provides information about the first instance of a filename that matches the file specified in the filespec argument.
long _findfirst( char *filespec, struct _finddata_t *fileinfo );
__int64 _findfirsti64( char *filespec, struct _finddata_t *fileinfo );
long _wfindfirst( wchar_t *filespec, struct _wfinddata_t *fileinfo );
__int64 _wfindfirsti64( wchar_t *filespec, struct _wfinddata_t *fileinfo );
Function Required Header Compatibility
_findfirst Win 95, Win NT
_findfirsti64 Win 95, Win NT
_wfindfirst or Win NT
_wfindfirsti64 or Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
If successful, _findfirst and _wfindfirst return a unique search handle identifying the file or group of files matching the filespec specification, which can be used in a subsequent call to _findnext or _wfindnext, respectively, or to _findclose. Otherwise, _findfirst and _wfindfirst return –1 and set errno to one of the following values:
ENOENT
File specification that could not be matched
EINVAL
Invalid filename specification
Parameters
filespec
Target file specification (may include wildcards)
fileinfo
File information buffer
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tfindfirst _findfirst _findfirst _wfindfirst
_tfindfirsti64 _findfirsti64 _findfirsti64 _wfindfirsti64
System Calls Routines
------解决思路----------------------
_find, _wfind Function Overview
_findnext, _findnexti64, _wfindnext, _wfindnexti64
Find the next name, if any, that matches the filespec argument in a previous call to _findfirst, and then alters the fileinfo structure contents accordingly.
int _findnext( long handle, struct _finddata_t *fileinfo );
__int64 _findnexti64( long handle, struct _finddata_t *fileinfo );
int _wfindnext( long handle, struct _wfinddata_t *fileinfo );
__int64 _wfindnexti64( long handle, struct _wfinddata_t *fileinfo );
Function Required Header Compatibility
_findnext Win 95, Win NT
_findnexti64 Win 95, Win NT
_wfindnext or Win NT
_wfindnexti64 or Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
If successful, _findnext and _wfindnext return 0. Otherwise, they return –1 and set errno to ENOENT, indicating that no more matching files could be found.
Parameters
handle
Search handle returned by a previous call to _findfirst
fileinfo
File information buffer
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tfindnext _findnext _findnext _wfindnext
_tfindnexti64 _findnexti64 _findnexti64 _wfindnexti64
System Calls Routines
------解决思路----------------------
_find, _wfind Function Overview
------解决思路----------------------
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt");
//读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字
system("dir /b /a-d /s c:\\*.* >d:\\allfilesinsub.txt");
//读文件d:\\allfilesinsub.txt的内容即C:\\下所有文件的名字包含子目录
system("dir /b /ad c:\\*.* >d:\\alldirs.txt");
//读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字
请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。
Linux下使用ls命令。
文章评论