zoukankan      html  css  js  c++  java
  • 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; //使用这个方法,可以保证每次输出一行数据
  • 相关阅读:
    css优化篇
    select超全超详细总结篇
    meta总结
    富文本编辑
    textarea 换行处理
    07 DRF响应类:Response
    06 内部类
    05 序列化组件
    04 APIView的请求生命周期
    python中if __name__ == '__main__'是什么?
  • 原文地址:https://www.cnblogs.com/xing901022/p/3500488.html
Copyright © 2011-2022 走看看