Well, this is what we got for writing this holidays at school using plain C and the most simple methods. Any solutions available?

Create program for defining hierarchical file-system structure in which node can be file or subdirectory with limitation that file can only be a leaf. Capabilities that program must have are:

- creating first subdirectoy or file (first child)
- creating next subdirectory or file (nex "brother")
- deleting leafs, subdirectories and files
- movement through filesystem structure to parent, first child and next "brother"
- printing directory contents
- constant location printing
- searching filesystem structure (function PREORDER())

Sorry for any bad english, it's translated text. Thank you for your time.

Cheers.

Recommended Answers

All 8 Replies

what compiler and operating system ?

>>Sorry for any bad english, it's translated text.
Looks ok to me. :)

what compiler and operating system ?

XP, Mingw in DevC++. Any ideas you have in mind? :)

10/10 for translation, 0/10 for effort

10/10 for translation, 0/10 for effort

I don't want from you to write the program, just some ideas how I could manage to write something like that.

win32 api CreateDirectory() to create a directory. To delete a directory you first have to delete all the files in it. FindFirstFile() and FindNextFile() will find all the files. Use it to create a linked list of the filenames then delete them one at a time.

Those suggestions will probably take you a few days to write, unless you are very good programmer now in which case you might do it in a couple hours or so. After that, come back, post the code you have written and we'll help you out some more.

I am back again. It happened to be that is no time-limit for this, and it's something completely different than I thought.

It must be something like a general tree type and there is not much examples for that on the net(or I don't know how to google that). It will be much easier if it's binary tree but that type is limited only to left and right.

Here is little part of code that I've written. I hope I can write this with a little step-by-step help by great people here:

#include <stdio.h>
#include <stdlib.h>
#define MAXNODES 100
#define LAMBDA -1
#define labeltype char

/* Needed functions for normal operating:
- INSERT CHILD(l,i,&T)
- DELETE(i,&T)
- ROOT(T)
- FIRST_CHILD(i,T)
- PARENT(i,T)
- LABEL(i,T)
- CHANGE_LABEL(l,i,&T)
- PREORDER 
*/

typedef int node;
typedef struct {
node root;
labeltype label[MAXNODES];
node parent[MAXNODES];
} TREE;

node NEXT_SIBLING(node i, TREE T){
node j,p;
p=T.parent[i];
for (j=i+1; j<MAXNODES; j++){
if (T.parent[j]==p) return j;
return LAMBDA;
}
}


int main(){
    
    
    
    system("pause");
    return 0;
}
typedef struct {node root;labeltype label[MAXNODES];node parent[MAXNODES];} TREE;

Use a node pointer pointing to childs rather. something simmilar to

struct node { 
    int data; 
    struct node* left; 
    struct node* right; 
}

What do you think about something like this? Still, I have to manage to add preorder search and deletenode function.

[LIST=1]
[*]#include <stdio.h>
[*]#include <stdlib.h>

[*]#define ISFILE 1
[*]#define ISDIR 2

[*]struct fnode{
[*] int nodetype;
[*] char* name;
[*] char* data;
[*] struct fnode* owner; 
[*] struct fnode* nextfile; 
[*] struct fnode* childlist; 
[*]};
[*]typedef struct fnode filenode; 

[*]void memfail(void);
[*]filenode* newfilenode(int, char*, char*);
[*]void addchildfile(filenode*,filenode*);
[*]void addnextfile(filenode*,filenode*);
[*]void printfilenode(filenode*, int);
[*]void printfilelist(filenode* fn,int level);
[*]void printfile(filenode* fn,int level);

[*]int main(void)
[*]{
[*]filenode* root;
[*]filenode *f1,*f2,*d3,*f4,*f31,*f32;

[*]root=newfilenode(ISDIR,"root","1234");

[*]f1 = newfilenode(ISFILE,"file1","100");
[*]f2 = newfilenode(ISFILE,"file2","200");
[*]d3 = newfilenode(ISDIR,"dir3","-300");
[*]f4 = newfilenode(ISFILE,"file4","400");
[*]f31 = newfilenode(ISFILE,"file31","1000");
[*]f32 = newfilenode(ISFILE,"file32","2000");

[*]addchildfile(root,f1);
[*]addchildfile(root,f2);
[*]addchildfile(root,d3);
[*]addchildfile(root,f4);

[*]addchildfile(d3,f31);
[*]addchildfile(d3,f32);

[*]printfile(root,0);
[*]system("pause");
[*]}

[*]filenode* newfilenode(int t,char *name,char *data)
[*]{filenode* fn;

[*]fn = malloc(sizeof *fn);
[*]if (fn==NULL) memfail;

[*]fn->nodetype = t;

[*]fn->name = name;
[*]fn->data = data;
[*]fn->owner = NULL;
[*]fn->nextfile = NULL;
[*]fn->childlist = NULL;
[*]return fn;
[*]}

[*]void printfilenode(filenode* fn,int indent)
[*]{
[*]if (fn==NULL) return;

[*]while (indent)
[*]{
[*] printf(" ");
[*] --indent; 
[*]};

[*]if (fn->nodetype==ISFILE)
[*] printf("F:");
[*]else
[*] printf("D:");

[*]printf("%-10s %10s",fn->name,fn->data);
[*]printf("\n");
[*]}

[*]void memfail(void)
[*]{ printf("Memory error\n");
[*] exit(EXIT_FAILURE);
[*]}

[*]void addnextfile(filenode* p,filenode* fn)
[*]{
[*]if (p==NULL)return;

[*]while (p->nextfile!=NULL)
[*] p = p->nextfile;

[*]p->nextfile = fn;
[*]}

[*]void addchildfile(filenode* p,filenode* fn)
[*]{
[*]if (p==NULL)return;

[*]if (p->childlist==NULL)
[*] p->childlist = fn;
[*]else
[*] addnextfile(p->childlist,fn);

[*]fn->owner = p;
[*]}

[*]void printfilelist(filenode* fn,int level)
[*]{
[*]while (fn!=NULL)
[*]{
[*] printfile(fn,level);
[*] fn = fn->nextfile;
[*]};
[*]}

[*]void printfile(filenode* fn,int level)
[*]{
[*]if (fn==NULL)return;
[*]printfilenode(fn,level);

[*]printfilelist(fn->childlist,level+1);
[*]}
[/LIST]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.