Hi everybody, I would need and advice. I work with binary tree in language C and I wanna to save data from binary tree to file. I try to make similar way to C++, but proposed code notice failure. How I modify it to put it into operation. that's the part of code with highlighted critical section: (thx for your reply)

void WriteToFile(char* file_name,ITEMTREE** binary_tree)
{
  FILE* file;
   if(fopen_s(&file,file_name,"w")!=0)
   {
     puts("failure\n");
   }
   else
   {
    WriteNodes(file,binary_tree);
    fclose(file);
   }
  }

void WriteNodes(FILE* file,ITEMTREE** binary_tree)
{
  if(binary_tree != NULL)
   {
    WriteNodes(**file**,&(*binary_tree)->left);
    fprintf(file,"%s %d\n",(*binary_tree)->surname,(*binary_tree)->ID_student);
    WriteNodes(file,&(*binary_tree)->right);
   }
}

What is the difference between line 19 and 21 ? Why **file** one time and just file the other time ?

You should also take a look at your code for the binary_tree. Normally when you use a class you would make the members private and use functions to acces them. And if you are using a class, why not make a nice function to get left and right instead of having to fiddle with pointers and pass a double pointer. Consider creating a Tree class which provides an interface to your ITEMTREE nodes.

If you are using a struct, look into classes and post your struct so we know how your pointers work. Also showing how you fill up your struct with data might help.

Also if you are having troubles with the pointers look into passing arguments by reference. Since you are using c++ you might aswell take advantage of it.

I don'use any class in programme (it's not OOP, it was written in language C, not C++), so it's all structured. I don't solve access to class and its items, bud correct functioning two functions which provide a write datas from data structure to file....

You need to post the structure. Is left and right pointers to ITEMTREE structures?

but proposed code notice failure

You need to be more specific about the erors. compiler errors? If so, what are they? Runtime errors? What is the behavior of the program?

This is the structure (it's correct, because it runs in function with binary tree's manipulation (AddNode,ViewNode,RemoveNode), only write into file doesn't run). Runtime error is shown always, concrete: Unhandled exception at 0x00dd1c1b in Binary.exe: 0xC0000005: Access violation reading location 0x00000024. Critical section is line 19 in previous code. I've tried to step by breakpoint and mentioned line is only executed in function - void WriteNodes(FILE* file,ITEMTREE** binary_tree)

typedef struct ItemTree
{
  long ID_student;
  char* name;
  struct ItemTree* right; 
  struct ItemTree* left; 
}ITEMTREE;

The only thing I see wrong with line 19 is **file** insted of just file.

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.