将二进制搜索树导出到.csv文件

时间:2019-04-30 20:39:54

标签: c++

我有一个BST数据库,我必须将其导出到.CSV文件。我可以得到它来导出第一条记录。我如何让它循环直到导出整个数据库。

void get(NodeActor* node, ofstream myfile)
{
if(node==NULL)
return;
myfile << "Year , Award, Winner, name, film"<< endl;
myfile << node->year << "," << node->award << "," << node->winner << "," << node->name << "," << node->film<< endl;
get(node->left,myfile);
get(node->right,myfile);
}
void ActorBST::ExportToCSV(NodeActor *node)
{
    string fileName;
    cout << "File Name(include .csv): ";
    cin >>fileName;
    ofstream myfile;
    myfile.open (fileName);
    myfile << "Year , Award, Winner, name, film"<< endl;
        myfile << node->year << "," << node->award << "," << node->winner << "," << node->name << "," << node->film<< endl;

    myfile.close();
}

1 个答案:

答案 0 :(得分:0)

我想您只需要在get方法内调用ExportToCSV函数。
您的get方法已经在处理递归,因此只要您将顶部节点传递到ExportToCSV,它就可以工作。

void ActorBST::ExportToCSV(NodeActor *node)
{
    string fileName;
    cout << "File Name(include .csv): ";
    cin >>fileName;
    ofstream myfile;
    myfile.open (fileName);
    get(node, myfile);
    myfile.close();
}

也许您想将get用作ActorBST类的方法,但这不是强制性的。