当前位置: 技术问答>linux和unix
(c语言)如何在程序里找出某一目录中的扩展名为plt的文件?
来源: 互联网 发布时间:2015-02-25
本文导语: 我想编一段c程序:在/mnt/usb目录里,找出扩展名为plt的文件?怎么做?给点提示或编程的思路。谢谢帮助! | 一个粗糙的版本, 有错请告知, 谢了, :-) void walkdir(const char *dirname) { ...
我想编一段c程序:在/mnt/usb目录里,找出扩展名为plt的文件?怎么做?给点提示或编程的思路。谢谢帮助!
|
一个粗糙的版本, 有错请告知, 谢了, :-)
void walkdir(const char *dirname)
{
char dname[PATH_MAX + 1];
int mode;
struct stat sbuf;
struct dirent *direntp;
DIR *dirp;
char *p;
int len;
if (lstat(dirname, &sbuf) != 0)
return ;
mode = sbuf.st_mode;
if (S_ISDIR(mode)) {
if ((dirp = opendir(dirname)) == NULL)
return ;
while ((direntp = readdir(dirp)) != NULL) {
if (strcmp(direntp->d_name, ".") && strcmp(direntp->d_name, "..")) {
strcpy(dname, dirname);
if (strcmp(dirname, "/"))
strcat(dname, "/");
strcat(dname, direntp->d_name);
walkdir(dname);
}
}
closedir(dirp);
return ;
} else {
if (S_ISREG(mode)) {
if ((len = strlen(dirname)) >= 4) {
p = (char *)(dirname + len - 4);
if (!strcmp(p, ".plt")) {
printf("%sn", dirname);
}
}
}
}
}
void walkdir(const char *dirname)
{
char dname[PATH_MAX + 1];
int mode;
struct stat sbuf;
struct dirent *direntp;
DIR *dirp;
char *p;
int len;
if (lstat(dirname, &sbuf) != 0)
return ;
mode = sbuf.st_mode;
if (S_ISDIR(mode)) {
if ((dirp = opendir(dirname)) == NULL)
return ;
while ((direntp = readdir(dirp)) != NULL) {
if (strcmp(direntp->d_name, ".") && strcmp(direntp->d_name, "..")) {
strcpy(dname, dirname);
if (strcmp(dirname, "/"))
strcat(dname, "/");
strcat(dname, direntp->d_name);
walkdir(dname);
}
}
closedir(dirp);
return ;
} else {
if (S_ISREG(mode)) {
if ((len = strlen(dirname)) >= 4) {
p = (char *)(dirname + len - 4);
if (!strcmp(p, ".plt")) {
printf("%sn", dirname);
}
}
}
}
}
|
static void findfile( void )
{
#define SUFFIX ".plt"
#define DIRRE "/mnt/usb"
int i = 0;
DIR *dirp; struct dirent *dp;
int suffixlen = strlen( SUFFIX );
dirp = opendir( DIRRE );
if( !dirp ){
perror("opendir");
return -1;
}
while( (dp = readdir(dirp)) != NULL ){
if( strncmp(dp->d_name+(dp->d_namlen - suffixlen),
SUFFIX, suffixlen) == 0 ){ /* find */
fprintf( stderr, "[%s] found.n", db->d_name );
}
}
(void)closedir( dirp );
}
{
#define SUFFIX ".plt"
#define DIRRE "/mnt/usb"
int i = 0;
DIR *dirp; struct dirent *dp;
int suffixlen = strlen( SUFFIX );
dirp = opendir( DIRRE );
if( !dirp ){
perror("opendir");
return -1;
}
while( (dp = readdir(dirp)) != NULL ){
if( strncmp(dp->d_name+(dp->d_namlen - suffixlen),
SUFFIX, suffixlen) == 0 ){ /* find */
fprintf( stderr, "[%s] found.n", db->d_name );
}
}
(void)closedir( dirp );
}