64,646
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include<cstring>
#include<iostream>
using namespace std;
#define MAX_LEN 1024
typedef struct csNode
{
char data[2048];
struct csNode *firstChild,*nextSibling,*parentNode;
}fileNode;
fileNode *GetFiles(char *strPath,fileNode *treeN);
fileNode *InsertSibling(fileNode *strRoot,fileNode *parentNode,char*fileName);
fileNode *InsertChild(fileNode *strRoot,char*fileName);
fileNode *CreateTree(char *strPath);
void PrintTree(fileNode *Node,int nLevel=0);
void DeleteTree(fileNode *Node);
int main()
{
struct csNode *root;
root = CreateTree("C:\\1234");
root = GetFiles("C:\\1234",root);
PrintTree(root,0);
DeleteTree(root);
system("pause");
return 0;
}
fileNode *CreateTree(char *strPath)
{
fileNode *pNode=(fileNode *)malloc(sizeof(fileNode));
pNode->firstChild=NULL;
pNode->nextSibling=NULL;
pNode->parentNode=NULL;
if(pNode==NULL)
{
printf("can not allocate\n");
return NULL;
}
else
{
strcpy(pNode->data,strPath);
}
return pNode;
}
fileNode *GetFiles(char *strPath,fileNode *treeN)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFile;
bool isSibling = false;
fileNode *node=treeN;
char szPath[MAX_LEN];
char szFind[MAX_LEN];
strcpy(szPath,strPath);
strcpy(szFind,strPath);
strcat(szFind,"\\*.*");
hFile = FindFirstFile(szFind,&FindFileData);
while(hFile!=INVALID_HANDLE_VALUE )
{
fileNode *p=NULL;
p=node;
fileNode *pcur=p;
char cfileName[MAX_PATH];
strcpy(cfileName,FindFileData.cFileName);
//文件夹
if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
if((strcmp(cfileName,".") != 0) &&(strcmp(cfileName,"..") != 0))//"."是当前路径,".." 是上一级的路径
{
if(isSibling==false)
{
p = InsertChild(p,cfileName);
}
else
{
p = InsertSibling(pcur,node,cfileName);
}
for(int i=0;i<strlen(szFind);i++)
szFind[i]=NULL;
strcpy(szFind,strPath);
strcat(szFind,"\\");
strcat(szFind,cfileName);
//strcat(szFind,"\\");
GetFiles(szFind,p);
}
}
//文件
else
{
if(isSibling==false)
{
p = InsertChild(p,cfileName);
}
else
{
p = InsertSibling(pcur,node,cfileName);
}
}
if(!FindNextFile(hFile,&FindFileData))//没有兄弟
{
hFile = INVALID_HANDLE_VALUE;
}
else//有兄弟
{
if((strcmp(cfileName,".") == 0) || (strcmp(cfileName,"..") == 0))
{
isSibling = false;
}
else
{
isSibling = true;//是兄弟
}
}
}
FindClose(hFile);
return node;
}
fileNode *InsertChild(fileNode *strRoot,char*fileName)
{
fileNode *pNode;
pNode=(fileNode *)malloc(sizeof(fileNode));
strRoot->firstChild=pNode;
pNode->parentNode=strRoot;
pNode->firstChild=NULL;
pNode->nextSibling=NULL;
strcpy(pNode->data,fileName);
return pNode;
}
fileNode *InsertSibling(fileNode *strRoot,fileNode *parentNode,char*fileName)
{
fileNode *pNode;
pNode=(fileNode *)malloc(sizeof(fileNode));
strRoot->nextSibling=pNode;
pNode->parentNode=parentNode;
pNode->firstChild=NULL;
pNode->nextSibling=NULL;
strcpy(pNode->data,fileName);
return pNode;
}
void PrintTree(fileNode *Node,int nLevel)
{
if(Node==NULL) return;
char buffer[100];
while(Node!=NULL)
{
strcpy(buffer,Node->data);
printf("%s\n",buffer);
PrintTree(Node->firstChild,nLevel + 1);//打印子节点
Node = Node->nextSibling; //打印兄弟节点
}
}
void DeleteTree(fileNode *Node)
{
if(!Node) return;
static fileNode *ptemp;
while(Node)
{
DeleteTree(Node->firstChild);
ptemp = Node;
Node = Node->nextSibling;
free(ptemp);
}
}