Help me write BST tree to file

Hi . I can only write one value to file, but I want all values written:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
get all, say for example barcode

char* bst:: get_bar(hwareItem*& root){

    while(root!=NULL){
        get_bar(root->left);
        get_bar(root->right);
        return root->barcode;
}
// write to file

void writehdware( bst& tree, char file[]){

    ofstream write;
    char* bar;

    write.open(file, ios::out);


    write<<tree.get_bar()<<endl;


    write.close();
}


gets only one value depending on traversal order
Do not return root->barcode, instead write it to the stream. So you need the stream as the parameter to get_bar(...)
thanks i try again...but pls tell me do I need to include writefile as part of class method? or separate method?
Last edited on
It's the difference between

void writehdware( bst& tree, char file[]){

and

void bst::writehdware(char file[]){


Since logic wise there's no difference it is up to you whether you want it be part of the class or not. boost for instance often separates the data from it's representation (as a file, string, etc.)
look at my code,,its not writing properly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
case 8: cout<<"Quiting Management and Control"<<endl;
                    cout<<"Written to file hardwares.txt"<<endl;
                    ofstream out;
                    out.open("hardwares.txt", ios::out);
                    hware_det.write(out);//struct type var is hware_det
                    out.close();
                    return 0;

//write function
void bst:: write( std::ofstream& out, hwareItem*& root){

    while(root!=NULL){
        write(out, root->left);
        out<<root->barcode<<endl;
        out<<root->description<<endl;
        out<<root->price_per_unit<<endl;
        out<<root->stock<<endl;
        write(out, root->right);
    }

}

void bst:: write(std::ofstream&out){

     write(out,root);

}
On line 12: replace while with if
Thanks! its working..
Last edited on
Topic archived. No new replies allowed.