如题所述
用 DOS 命令 DIR 就可以。
假定文件夹路径 D:\\zzz\\path\\temp
所有文件 * 号
转向到 a.log
system("dir D:\\zzz\\path\\temp\\* > a.log");
打开 a.log
读出倒数第2行 文件个数
123 File(s) 4,567 bytes
文件个数 为 0 就是空的
#include <iostream>
using namespace std;
#include <stdio.h>
int main(){
FILE *fp;
char str[200];
int i,n=0;
int nf, ns;
system("dir D:\\zzz\\path\\temp\\* > a.log");
fp=fopen("a.log","r");
while(1){
if (fgets(str,200,fp)==NULL) break;
n++;
}
rewind(fp);
printf("n=%d\n",n);
for (i=0;i<n-1;i++) fgets(str,200,fp);
// printf("%s",str);
sscanf(str," %d",&nf);
if (nf == 0) printf("empty dir \n");
else printf("Number of files: %d\n",nf);
fclose(fp);
return 0;
}
假定文件夹路径 D:\\zzz\\path\\temp
所有文件 * 号
转向到 a.log
system("dir D:\\zzz\\path\\temp\\* > a.log");
打开 a.log
读出倒数第2行 文件个数
123 File(s) 4,567 bytes
文件个数 为 0 就是空的
#include <iostream>
using namespace std;
#include <stdio.h>
int main(){
FILE *fp;
char str[200];
int i,n=0;
int nf, ns;
system("dir D:\\zzz\\path\\temp\\* > a.log");
fp=fopen("a.log","r");
while(1){
if (fgets(str,200,fp)==NULL) break;
n++;
}
rewind(fp);
printf("n=%d\n",n);
for (i=0;i<n-1;i++) fgets(str,200,fp);
// printf("%s",str);
sscanf(str," %d",&nf);
if (nf == 0) printf("empty dir \n");
else printf("Number of files: %d\n",nf);
fclose(fp);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2014-01-24
如果一个目录是空的,输出为2, 即只有. 和..目录
#include <sys/types.h>#include <dirent.h>
#include <stdio.h>
int main(int argc , char **argv)
{
DIR *dirp;
int num=0;
dirp = opendir(argv[1]);
while (dirp) {
if ( readdir(dirp) != NULL)
++num;
else
break;
}
closedir(dirp);
printf("%d\n",num);
}
第2个回答 2014-01-23
PathIsDirectoryEmpty( char *folder );
需要include <Shlwapi.h>,以及导入库Shlwapi.lib
需要include <Shlwapi.h>,以及导入库Shlwapi.lib