实现的打印文件目录树,但是添加节点错误。

SureGOGOGO 2016-10-24 05:34:55
p = InsertSibling(pcur,node,cfileName); 这个地方应该写错了,但是一时想不出来。
#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);
}

}
...全文
131 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ipqtjmqj 2016-10-25
  • 打赏
  • 举报
回复
引用 4 楼 lishunihaoa 的回复:
[quote=引用 3 楼 ipqtjmqj 的回复:] [quote=引用 2 楼 lishunihaoa 的回复:] [quote=引用 1 楼 ipqtjmqj 的回复:] fileNode *p=NULL; p=node; 这句放到while外面
对哦,我搞错了,昨天头脑短路了。。。,谢谢啦fileNode *InsertSibling(fileNode *strRoot,fileNode *parentNode,char*fileName);这个函数,每次只有while的循环只需要记录的是上一次的fileNode *strRoot,因为插入的是兄弟节点,还没有退出while循环,而这两个fileNode *p=NULL; p=node;是新的文件夹搜索了 ,但是还有个问题是,节点太多时候,程序会奔溃。等我解决马上结贴。 [/quote] 结点多时崩溃,应该是栈溢出了,你把szPath, szFind两个变量放到堆上[/quote] 问题解决了,虽然不是你说的这个,但是还是谢谢你的帮助,问题在于 char buffer[100]; strcpy(buffer,Node->data); 因为data是个2048的数组,可能是文件名称过长了,导致数组越界了。把char buffer[100]; 改成 char buffer[2048];问题解决。[/quote] 嗯,现在内存都很大,一般够用,越不了界的。我看你在c++版本提的问题,可是代码都是c语言的, 其实处理字符串用c++的std::string很方便,内存都自动分配,转成c语言的字符串有.c_str()方法。
SureGOGOGO 2016-10-25
  • 打赏
  • 举报
回复
引用 3 楼 ipqtjmqj 的回复:
[quote=引用 2 楼 lishunihaoa 的回复:] [quote=引用 1 楼 ipqtjmqj 的回复:] fileNode *p=NULL; p=node; 这句放到while外面
对哦,我搞错了,昨天头脑短路了。。。,谢谢啦fileNode *InsertSibling(fileNode *strRoot,fileNode *parentNode,char*fileName);这个函数,每次只有while的循环只需要记录的是上一次的fileNode *strRoot,因为插入的是兄弟节点,还没有退出while循环,而这两个fileNode *p=NULL; p=node;是新的文件夹搜索了 ,但是还有个问题是,节点太多时候,程序会奔溃。等我解决马上结贴。 [/quote] 结点多时崩溃,应该是栈溢出了,你把szPath, szFind两个变量放到堆上[/quote] 问题解决了,虽然不是你说的这个,但是还是谢谢你的帮助,问题在于 char buffer[100]; strcpy(buffer,Node->data); 因为data是个2048的数组,可能是文件名称过长了,导致数组越界了。把char buffer[100]; 改成 char buffer[2048];问题解决。
ipqtjmqj 2016-10-25
  • 打赏
  • 举报
回复
引用 2 楼 lishunihaoa 的回复:
[quote=引用 1 楼 ipqtjmqj 的回复:] fileNode *p=NULL; p=node; 这句放到while外面
对哦,我搞错了,昨天头脑短路了。。。,谢谢啦fileNode *InsertSibling(fileNode *strRoot,fileNode *parentNode,char*fileName);这个函数,每次只有while的循环只需要记录的是上一次的fileNode *strRoot,因为插入的是兄弟节点,还没有退出while循环,而这两个fileNode *p=NULL; p=node;是新的文件夹搜索了 ,但是还有个问题是,节点太多时候,程序会奔溃。等我解决马上结贴。 [/quote] 结点多时崩溃,应该是栈溢出了,你把szPath, szFind两个变量放到堆上
SureGOGOGO 2016-10-25
  • 打赏
  • 举报
回复
引用 1 楼 ipqtjmqj 的回复:
fileNode *p=NULL; p=node; 这句放到while外面
对哦,我搞错了,昨天头脑短路了。。。,谢谢啦fileNode *InsertSibling(fileNode *strRoot,fileNode *parentNode,char*fileName);这个函数,每次只有while的循环只需要记录的是上一次的fileNode *strRoot,因为插入的是兄弟节点,还没有退出while循环,而这两个fileNode *p=NULL; p=node;是新的文件夹搜索了 ,但是还有个问题是,节点太多时候,程序会奔溃。等我解决马上结贴。
ipqtjmqj 2016-10-24
  • 打赏
  • 举报
回复
fileNode *p=NULL; p=node; 这句放到while外面

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧