
.c文件 随意
变量 随意
根据实际需要 可以任意多 几千 几万 几亿都可以
主函数 一个
多了 就出错。
1 最简单的利用dir 命令#include<stdio.h>
int main(void)
{
char *filepath = "c:"/* 这里为你要设置的绝对路径 */
char f[400] = "dir "
strcat(f,filepath)
system(f)
system("pause")
return 0
}
2---
#include <stdio.h>
#include <io.h>
//节点
typedef struct FileName
{
char Name[500]//定义文件名上限为500
struct FileName *next
}node
//链表头指针
node *Head = NULL
//释放函数
int Free()
{
int count = 0
node *NowNode = Head
while(NowNode->next)
{
Head = Head->next
free(NowNode)
NowNode = Head
count++
}
free(Head)
Head = NULL
count++
return count
}
int main()
{
struct _finddata_t c_file
long hFile = 0
node *NewNode = NULL
node *np = NULL//指向头节点的指针
int sum = 0//目标路径下的文件数目
if((hFile = _findfirst("c:\\*.*"/*设置路径和文件类型*/,&c_file)) == -1l)
{
printf("There is no file in current directory!")
}
else
{
//处理第一个文件
NewNode = (node *)malloc(sizeof(node))
strcpy(NewNode->Name,(c_file.name))
NewNode->next = NULL
Head = NewNode
//处理其他文件
while((_findnext(hFile,&c_file)) == 0)
{
NewNode = (node *)malloc(sizeof(node))
strcpy(NewNode->Name,(c_file.name))
NewNode->next = Head
Head = NewNode
}
}
//输出所有文件名
np = Head
while(np)
{
printf("%s\n",np->Name)
np=np->next
sum++
}
printf("该路径下共有%d个文件\n",sum)
sum = Free()
printf("%d个节点被释放",sum)
getchar()
return 0
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)