
可以利用for循环,像Linux
dir=opendir
(dirname)打开文件夹,返回目录指针,dp=readdir(dir)利用读目团咐录,返回一行行读取目录的文件结构体指针,指针中存的有文件属性,是文件,还是文件夹。
通过判链或好断是文件或棚铅者文件夹:
如果是文件,就就输出文件名dp->name
否则,就是一个文件夹
继续dir1=opendir(dp->name),dp1=readdir(dir1)..
一直循环到判断不到目录了。
windows应该也有类似的函数吧,这个我就么有用过了,你找找。
档闷埋 C#获取文件夹名和文件个数,参考代码如下:
DirectoryInfo di = new DirectoryInfo(@"文件夹路径")di.GetDirectories()//获取子文件夹列表
di.GetFiles()//获取文件列表
如果嵌套文件夹个数较多,可以使用子线程统计,参考代码如下:
罩虚int count = 0public void GetFilesCount(string path)
{
//开子线程去统计
count += System.IO.Directory.GetFiles(path).Length
foreach (var folder in System.IO.Directory.GetDirectories(path))
{
count += GetFilesCount(folder)
} 行蚂
}
楼主你好具体代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 40
int main()
{
int i,count = 0
char *cSource,*cSearch
FILE *fp
cSource = (char *)malloc(N * sizeof(char))
cSearch = (char *)malloc(3 * sizeof(char))
if((fp = fopen("word.txt", "r")) == NULL)
{
printf("仔拍文乱戚或件打开失败!\n")
exit(0)
}
printf("输入统计的汉字:")
scanf("%s", cSearch)
fgets(cSource, N, fp)
for(i = 0i <(int)strlen(cSource)i++)
{
if(cSource[i] == cSearch[0] &&cSource[i+1] == cSearch[1])
//一个汉字占两个字节哗伍 所以需要判断两个字节的内容
count++
}
printf("%d\n", count)
return 0
}
如果word.txt中包含的内容为:你好吗 你 你
输入:你
输出:3
希望能帮助你哈
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)