I have a function printtree which prints out a tree. I searched on many places on the internet and I found that mmap would be a good way to allocate memory and write into a file.
I want to use mmap to print the contents of the printtree into a file. I am learning mmap for the first time and I am not sure how to do it.
printtree() is as follows:
void printtree(struct Node *root, int indent)
{
int i;
if(root != NULL)
{
printtree(root->right,indent + 1);
for(i = 0;i<indent;i++)
{
printf("\t");
}
printf("%d\n",root->key);
printtree(root->left,indent + 1);
}
}
In my main.c, I am trying this:
struct Node *root = NULL;
int info ;
int *arr;
int choice;
int fd;
int mode = 0x0777;
struct stat mystat;
void *p;
int size;
FILE *file_ptr;
if (argc != 2){ printf("Error\nIncorrect number of arguments\n");exit(1);}
if (access(argv[1],F_OK) != -1)
{
fd = open(argv[1],O_RDWR);
if (fd == -1)
{
perror("open");
exit(1);
}
if(fstat(fd, &mystat)<0)
{
perror("fstat");
close(fd);
exit(1);
}
p = mmap(0,mystat.st_size , PROT_READ | PROT_WRITE,
MAP_SHARED ,fd,0);
if(p == MAP_FAILED)
{
perror("mmap");
close(fd);
exit(1);
}
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Lookup\n");
printf("4.Print\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value to be inserted : ");
scanf("%d", &info);
//write(fd,&info,36);
if( (root = insert(root,info)) == 0){}
else
break;
case 2:
printf("Enter the value you want to delete : ");
scanf("%d",&info);
root = deleteNode(root,info);
break;
case 4:
printtree(root,0);
break;
//printf("%s",arr[0]);
printf("\n");
break;
case 5:
close(fd);
exit(1);
case 3:
printf("Enter the element you want to look for : ");
scanf("%d",&info);
Search(root,info);
break;
case 6:
printtree(root,0);
break;
default:
printf("Wrong choice\n");
}
}
}