0

I'm trying to define this inline function. It would create a decision tree (a particoular binary tree used in operations research).

This is the code:

inline void printTreeFile(int* i_node,int* j_node,int* q_infr,int* value,int nStart,int level,std::fstream& tree)
{       int spazi=0,len,stop;   
        for(int actual_level=0;actual_level<level;actual_level++)
            {spazi+=9;
            if(i_node[actual_level]==0)
                len=1;
            else
                len=(int)log10(i_node[actual_level])+1;
            spazi+=len;
            if(j_node[actual_level]==0)
                len=1;
            else
                len=(int)log10(j_node[actual_level])+1;
            spazi+=len;
            if(q_infr[actual_level]==0)
                len=1;
            else
                len=(int)log10(q_infr[actual_level])+1;
            spazi+=len;
            }
        if(value[level]==0)
            {
            tree<<"-->("<<i_node[level]<<","<<j_node[level]<<","<<q_infr[level]<<"=1)";
            tree<<endl;
            for(int i=0;i<spazi;i++)
                tree<<" ";
            tree<<"-->("<<i_node[level]<<","<<j_node[level]<<","<<q_infr[level]<<"=0)";
            }
        else
            tree<<"-->("<<i_node[level]<<","<<j_node[level]<<","<<q_infr[level]<<"=1)";
        //cin>>stop;
}

The std::fstream& tree is defined in main as:

fstream tree;                       
remove("tree");
tree.open("tree",ios::out|ios::app);

I don't understand why the file tree is empty until is executed the command tree<<endl;

I need to see how the tree evolve itself without waiting for that command...anyone know a solution to this problem? thanks to all that will answer!

4

1 回答 1

0

endl输出换行符并强制缓冲区刷新,这就是您在那时看到它的原因。

除非您修改它们的属性,否则文件流是完全缓冲的,因此在缓冲区已满或您明确刷新之前您将看不到任何内容。

这可以通过endl(假设您希望先发送换行符)、通过ostream::flush或在关闭文件时完成。

可能最简单的解决方法是将您的if声明更改为:

if (value[level] == 0) {
    tree << "-->(" << i_node[level] << "," << j_node[level] << "," << q_infr[level] << "=1)\n";
    for (int i = 0; i < spazi; i++)
        tree << " ";
    tree << "-->(" << i_node[level] << "," << j_node[level] << "," << q_infr[level] << "=0)";
} else {
    tree << "-->(" << i_node[level] << "," << j_node[level] << "," << q_infr[level] << "=1)";
}
tree.flush();

事实上,鉴于其中一些行的共性,我正在考虑对其进行重构以使其更具可读性:

inline void outLine (
    std::fstream& tree,
    int           *inode,
    int           *jnode,
    int           *qinfr,
    int           level,
    int           val)
{
    tree << "-->(" << i_node[level] << "," << j_node[level]
        << ","    << q_infr[level] << '=' << val << ')';
}

:

if (value[level] == 0) {
    outLine (tree, i_node, j_node, q_infr, level, 1);
    tree << '\n';
    for (int i = 0; i < spazi; i++)
        tree << " ";
    outLine (tree, i_node, j_node, q_infr, level, 0);
} else {
    outLine (tree, i_node, j_node, q_infr, level, 1);
}
tree.flush();

请记住为什么文件倾向于完全缓冲而标准输出是行缓冲的原因。在工作完成之前您不关心的大多数文件因此完全缓冲更有效。在这种情况下显然不是这种情况,但是比必要更频繁地刷新通常会减慢您的代码速度。

如果,如您所说,这只是调试代码,您可以忘记重构或过度刷新的速度损失。

于 2013-08-23T08:31:40.723 回答