|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
) S0 g8 b2 n/ ]8 J8 G) ^8 e+ `[mw_shl_code=cpp,true]#include <string> j: F( G* E8 ] I: l( N
#include <io.h>
% q! i. }, V0 k% l#include <vector>; M( U5 ~5 O/ T
#include <iostream>, v; ], N9 j% F4 h% u
#include <fstream>& C$ }# V5 O! _2 [3 _
using namespace std;! [+ Z, V' {/ \ R# \( V/ K; `
/ _( Q7 H" ^2 h$ [5 y
$ t0 M* m: H' K9 M( ~% G# b
/************************************************************************/
2 C" u4 T" ?: J8 r. K( C/* 获取文件夹下所有文件名
% \+ r: I6 B8 \% J3 ?2 X g 输入:
8 e2 E6 M9 l! r7 \5 N( @8 c path : 文件夹路径
# A0 M9 ^/ o; C+ B1 Y exd : 所要获取的文件名后缀,如jpg、png等;如果希望获取所有4 Y; y9 q+ j) K/ G. b5 M, p5 B
文件名, exd = ".jt"
9 X5 M- e* x6 @; H 输出:
' q* ?: ^1 U) {* G9 W/ J files : 获取的文件名列表 含有路径
) ^$ k% u% h% H* m2 o/ U. g. _*/5 i1 G/ N! \% r
/************************************************************************/7 L9 V0 w; P( q/ U+ `/ ^
- h- y6 i$ S8 Hvoid CONFIG_getFiles( string path, string exd, vector<string>& files )
" i# p0 m$ j6 R) U* a. a: R{
+ [+ i7 k* A" G ] //文件句柄
" z9 S4 Q1 K* @ long hFile = 0;. |. O6 z4 i9 F V, P5 I6 G) I) m: g1 t1 O
//文件信息( T3 ^8 g% _& M; R1 v# B% y! w
struct _finddata_t fileinfo;: B+ X" E( o/ G& i/ X* S
string pathName, exdName; M0 n, `( R0 s
: W5 B9 B4 y6 _
string p;
2 ~% V* j8 A( u: \5 w' c h if((hFile = _findfirst(pathName.assign(path).append("\\*").c_str(),&fileinfo)) != -1)/ y( ]" |3 k- |2 x/ D
{, |* @; i: c8 K
do# b' ^6 Y" L9 c% q
{
% B, }% D i2 t3 f //如果是文件夹中仍有文件夹,迭代之( p& x: h* [2 [8 f* G1 ?
//如果不是,加入列表0 y7 `+ U' f) [1 F. Z# F. H8 b
// 不推荐使用,硬要使用的话,需要修改else 里面的语句# o4 j T# k d6 K' z$ q* _0 L# o2 [
if((fileinfo.attrib & _A_SUBDIR))
* u- A% o. I! ]' u" n3 r d {
) i2 ?3 _7 G% Z' ]7 t
! P0 A" h! x, v# X1 g if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0). [" _3 @" ]/ X& z0 Y$ J1 V
CONFIG_getFiles( pathName.assign(path).append("\\").append(fileinfo.name), exd, files );
$ f( e% ?9 U; Y7 P/ Y
5 C8 f, z: m3 a2 Z5 ?# W/ k }+ ] j$ ~/ c0 T4 P% v
else
% U9 r, w) P5 w {
3 c# s' z$ q) _! g# ` if (strstr(fileinfo.name,exd.c_str()))
/ G" }4 }* P, }5 N( x8 |: W. b) d {
- ^* X* Y U3 L8 ]2 M1 ?; W files.push_back(pathName.assign(path).append("\\").append(fileinfo.name)); // 要得到绝对目录使用该语句
+ S0 z7 ? o& S- y; C& n }
& B8 z0 h2 Y+ E; P9 Z. n' t6 p/ Y; {; l% }( e& u M9 }
//如果使用
! x. V! A1 y/ @7 ^) I // files.push_back(fileinfo.name); // 只要得到文件名字使用该语句
5 N O, p: |' Q, w& T }
. }6 ?5 l. X9 l( E9 J; ]1 c) r' n+ n }while(_findnext(hFile, &fileinfo) == 0);/ T# b% a# h, E- J
_findclose(hFile);7 v. _% c% h( D$ O9 p8 w( b
}
) w: i# M- N$ e) w. L& x- T}6 ?& X( {- E7 C
[/mw_shl_code]; W* k3 D, U7 I# H
|
|