so these are my structures:

struct information
{
int Id;
char name[20];
}

that structure I use in my tree structure

struct node{
information *employee;
node *left,*right
}

and I have a generating function:

node* generate_tree_from_file(void)
{
node* tree=malloc(sizeof(node));
employee* emp=malloc(sizeof(employee));
int NoEmployees;
FILE *input_file;
input_file=fopen("emp.bin","r");
if(input_file)

{
		fread(&NoEmployees,sizeof(int),1,input_file);
		for(int i=0;i<NOEmployees;i++)
		{
			fread(&employee,sizeof(employee),1,input_file);
//Insert prototype: node* Insert(struct Employee*,struct node*)
//Insert @param: first param. is a pointer to a struct of type employee,second one is //the tree in which we want to insert
                        Insert(employee,tree);
		}
		fclose(fisier);
	}
return tree;

and this is how I use it in my main function:

int main()

...
node* Tree;
Tree=generate_tree_from_file();
...

I don't understand why the value of Tree in main is NULL

Recommended Answers

All 5 Replies

One possibility is that employee.bin file does not exist or is empty.

You open the file in text mode but read it as if it were open in binary mode. On line 7 open the file in binary mode input_file=fopen("emp.bin","rb"); You didn't post struct employee so we have no idea what it contains.

One possibility is that employee.bin file does not exist or is empty.

You open the file in text mode but read it as if it were open in binary mode. On line 7 open the file in binary mode input_file=fopen("emp.bin","rb"); You didn't post struct employee so we have no idea what it contains.

the struct employee:

struct employee
{
char name[20];
char JobTitle[20];
float salary;
float bonuses;
int CardID;

the project requirements state that the tree structure be created from a file and saved to a file
because I'm thinking that reading directly the tree from the file might be difficult because the tree contains direction pointers, I've decided to read each employee structure and do an insertion(the number of nodes will be the first thing written to the file)

Few problems here.
First, there's only one struct employee allocated (line 4), and would be written over and over with each new record.
Second, I said "would be" above, because &emp in the fread is wrong; it should be just emp. Can you see the difference?
Third, it is very much suspicious that the elements of tree (line 3) are not initialized in any way. I bet that it confuses Insert big time.

PS: you are absolutely correct that reading the whole tree from the file is wrong.

Few problems here.
First, there's only one struct employee allocated (line 4), and would be written over and over with each new record.
Second, I said "would be" above, because &emp in the fread is wrong; it should be just emp. Can you see the difference?
Third, it is very much suspicious that the elements of tree (line 3) are not initialized in any way. I bet that it confuses Insert big time.

Well, my reasoning for allocating only one structure, is that I read it from the file and insert it(repeating this step "NoNodes" times).
About the "&" part (I'm assuming that is the same case as reading a string with scanf), well I can't grasp the difference in both cases.

> I read it from the file and insert it
Unless your Insert() duplicates the structure, all the nodes will point to the same place.

> I can't grasp the difference
Consider the following:

struct employee emp1;
struct employee * emp2 = malloc(...);
fread(&emp1, ...);
fread(emp2, ...);

emp1 is a structure, and we pass its address. emp2 is an address already. fread(&emp2, ...) would fill up not the memory allocated and pointed to by emp2, but the memory occupied by the variable emp2 itself.

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.