Files and Folders using Data structure #include<stdio.h> #include<string.h> #include<stdlib.h> #include<conio.h> #include<dir.h> #include<dos.h> struct node { char *name; /* used to get file / folder name. */ int attrib; /* used to get it's attribute. */ struct node *next; /* concept of Linked list */ }; void main() { struct node *head,*head1; struct node *list,*list1; struct node * place(struct ffblk ff,struct node *first,int don); void display(struct node *first); void print(struct node *list,int *i); int i,c,c1,done,done1; struct ffblk f,f1; head=NULL; head1=NULL; clrscr(); done=findfirst("*.*",&f,FA_DIREC|FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_ARCH); /* struct variable "f" contains all files and folders information */ done1=findfirst("*.*",&f1,FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_ARCH); /* struct variable "f1" contains all files information */ head=place(f,head,done); /* content of f is placed in struct head */ display(head); /* Note : f contains name of files and folders with their attributes in f.ff_name, f.ff_attrib which is assigned to name, attrib in the struct node */ printf(" ************************************************* "); getch(); head1=place(f1,head1,done1); /* content of f1 is placed in struct head1 */ display(head1); /* Note : f1 contains name of files and folders with their attributes in f1.ff_name, f1.ff_attrib which is assigned to name, attrib in the struct node */ printf(" ************************************************* "); getch(); i=0; c1=0; /* Here, head and head1 are compared so that we could extract only the folders. */ list=head; /* head is assigned to list */ while(list!=NULL) { list1=head1; /* head1 is assigned to list1 */ if(list1==NULL) /* if there are 0 files */ print(list,&i); /* then display content of list */ else { while(list1!=NULL) { if(strcmp(list->name,list1->name)==0) /* compare list and list1 */ c1=1; list1=list1->next; } if(c1==0) /* if folder found both in list and list1*/ print(list,&i); /* then display content of list */ } c1=0; list=list->next; } printf(" FOLDERS = %d",i); printf(" ************************************************* "); printf(" Where,"); printf(" H - Hidden"); printf(" D - Directory"); printf(" R - Read only"); printf(" S - System"); printf(" A - Archive"); getch(); free(list1); free(list); free(head); free(head1); } void print(struct node *list,int *i) { void property(struct node *list); /* to display folders other than default folders (. and ..) */ if((strcmp(list->name,"."))!=0 && (strcmp(list->name,".."))!=0) { *i=*i+1; /* counts number of folders */ property(list); printf(" %s ",list->name); } } void property(struct node *list) { /* finds their attribute */ if(list->attrib & FA_HIDDEN) printf("(H)"); if(list->attrib & FA_DIREC) printf("(D)"); if(list->attrib & FA_RDONLY) printf("(R)"); if(list->attrib & FA_SYSTEM) printf("(S)"); if(list->attrib & FA_ARCH) printf("(A)"); } struct node * place(struct ffblk ff,struct node *first,int don) { static int j; void create(struct node *first,char *ch,int d); void insert(struct node *first,char *ch,int d); int i=0,c=0; char *p; if(don!=0) first=NULL; else { while(don==0) { if(i==0) { first=(struct node *)malloc(sizeof(struct node)); if ((p = (char *) malloc(14)) == NULL) exit(1); strcpy(p,ff.ff_name); create(first,p,ff.ff_attrib); i=1; } else { if ((p = (char *) malloc(14)) == NULL) exit(1); strcpy(p,ff.ff_name); insert(first,p,ff.ff_attrib); } don=findnext(&ff); c=c+1; } } if(j==0) { printf(" ************************************************* "); printf(" %d FILES & FOLDERS ",c); j+=1; } else printf(" %d FILES ",c); return(first); } void create(struct node *first,char *ch,int d) { char *p; if ((p = (char *) malloc(sizeof(ch))) == NULL) exit(1); p=ch; first->name=p; first->attrib=d; first->next=NULL; return; } void insert(struct node *first,char *ch,int d) { struct node *temp,*list; char *p; list=first; while(list->next!=NULL) list=list->next; if ((p = (char *)malloc(sizeof(ch))) == NULL) exit(1); p=ch; temp=(struct node *)malloc(sizeof(struct node)); temp->name=p; temp->attrib=d; temp->next=NULL; list->next=temp; return; } void display(struct node *first) { struct node *list; void property(struct node *list); list=first; if(list==NULL) printf(" NULL"); else { while(list->next!=NULL) { property(list); printf("%s %d ",list->name,list->attrib); list=list->next; } property(list); printf("%s %d ",list->name,list->attrib); } return; }
No comments: