c++中智能輸出文件

首先咱們要爲每一時間步,設置一個文件名:ios

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

這樣傳入的整型時間步,就能夠添加到輸出文件名中;spa

 

而後,輸出文件:code

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

這裏面,精簡下來其實就是blog

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; //使用這個方法,能夠保證每次輸出一行數據
相關文章
相關標籤/搜索