![](/img/trans.png)
[英]There is a matrix in a .txt file, how can i get the rows and columns from it? C++
[英]How to save looped output in C++ to a .txt file with columns and rows
我是 C++ 新手,我想保存在循环多个文件时打印的输出。 输出是 3 个浮点数,称为mass_pole
、 sum
和sum_SM
。 如何使用正确的 C++ 选项卡/新行语义将这些循环输出(不覆盖它们)保存到 a.txt 文件。 我想要类似的东西:
//mass_pole //sum //sum_SM
200 2300.2 713.4
250 2517.3 1231.77
300 2110.5 432.5
在 a.txt 文件中(没有标题)。
到目前为止,我的代码如下,但我认为这不符合我上述的愿望。 具体来说,我需要 L25 以后的帮助。 任何建议将不胜感激!
// root -l aTGCs_test.C
#include <iostream>
#include <fstream>
#include <string>
#include "TMath.h"
using namespace std;
void masspoleweights(TString file, TString mass_pole) {
TChain* myTrees = new TChain("Events");
[. . .]
for (int j = 0; j < myTrees->GetEntries(); ++j){
//for (int j = 0; j < 1000; ++j){
myTrees->GetEntry(j);
sum=variable1;
sum_SM=variable2;
}
L25>> ofstream myfile ("weights.txt");
//saving outputs to file (three outputs that loop by mass: mass, sum, pdfweights)
for (int i = 0; i < 200; ++i){
myfile << mass_pole << sum << sum_SM << std::endl;
}
}
根据评论部分所有专家的帮助,这是最终有效的代码:
ofstream myfile ("weights.txt", ios::in | ios::out | ios::app); //file open mode (READ), WRITE, append
//loop over all outputs to save in a txt file (no overwrites):
for (int i = 0; i < 1; ++i){
ofstream::ios_base::app;
myfile << mass_pole << "\t" << sum << "\t" << sum_SM << "\n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.