c++中智能输出文件

首先我们要为每一时间步,设置一个文件名:

  char timestr[10] = "1";
    itoa(time,timestr,10);
    std::string s;
    s += timestr;
    std::string path = "test_"+s+".txt";

这样传入的整型时间步,就可以添加到输出文件名中;

 

然后,输出文件:

    std::ofstream out(path,std::ios_base::ate);
    std::ofstream out1(path1,std::ios_base::ate);
    int num[300]={0};
    for(k=0;k<mn;k++){
        for(i=0;i<ms;i++){
            if(h_set[i*mn+k] ==1 ){
                for(j=0;j<mn;j++){
                    if(h_set[i*mn+j] == 1 && j != k)
                        neighbour.push_back(j);
                }
            }
        }
        out<<k<<" "<<neighbour.size()<<" ";
        num[neighbour.size()]++;
        for(i=0;i<neighbour.size();i++)
        {
            out<<neighbour[i]<<"(1)"<<" ";
        }
        out<<std::endl;
        neighbour.clear();
    }
    for(k=0;k<300;k++){
        out1<<k<<" "<<num[k]<<std::endl;
    }

这里面,精简下来其实就是

std::ofstream out(path,std::ios_base::ate);
out<<k<<" "<<neighbour.size()<<" ";
        
    for(i=0;i<neighbour.size();i++)
    {
        out<<neighbour[i]<<"(1)"<<" ";
    }
out<<std::endl; //使用这个方法,可以保证每次输出一行数据

 

posted @ 2014-01-01 12:02  xingoo  阅读(401)  评论(0编辑  收藏  举报