linux中类似findfirst的函数,findfirst findnext函数解决思路

这篇博客介绍了在C语言中,当findfirst和findnext函数不可用时,如何使用_findfirst和_findnext函数来实现文件查找。文章提供了示例代码,并指出在Windows环境下,可以使用_findfirst系列函数来代替。还提醒开发者,在处理文件和目录时,考虑使用shell命令可能更为稳妥,特别是在处理复杂情况时。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当前位置:我的异常网» 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命令。

文章评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值