c ++ intelligent output file

First of all we want to each time step, setting a file name:

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

Such incoming integer time steps, can be added to the output file name;

 

Then, the output file:

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;
    }

There is, in fact, slimmed down

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; //使用这个方法,可以保证每次输出一行数据

 

Reproduced in: https: //my.oschina.net/u/204616/blog/545406

Guess you like

Origin blog.csdn.net/weixin_34346099/article/details/91989327