Monday, 15 February 2010

c++ - I am trying to write out the results of a process to a text file, the issue I have is with the alignment of the columns -



c++ - I am trying to write out the results of a process to a text file, the issue I have is with the alignment of the columns -

i sure somewhere has had same issue have looked far , wide (including here on stackoverflow) find out how align columns in output file. next finish code using (for event generator called pythia 8 of c++ primary language):

using namespace pythia8; int main() { pythia pythia; pythia.readstring("top:gg2ttbar = 1"); pythia.init(2212, 2212, 14000.); ofstream myfile; myfile.open("ttbar.txt"); (int ievent = 0; ievent < 1; ++ievent) { if (!pythia.next()) continue; vector<double> part; (int = 0; < pythia.event.size(); ++i) { if (pythia.event[i].status() == 91) part.push_back(i); } myfile << "n = " << part.size() << endl; (int j = 0; j < (int(part.size()) - 1); ++j) { myfile << left << setw(4) << int(part[j]); myfile << setw(4) << left << pythia.event[part[j]].name() << " " << right << pythia.event[part[j]].id() << " " << pythia.event[part[j]].px() << " " << pythia.event[part[j]].py() << " " << pythia.event[part[j]].pz() << " " << pythia.event[part[j]].m() << " " << pythia.event[part[j]].pt() << endl; } } pythia.stat(); myfile.close(); homecoming 0; }

the issue occurs near bottom loop writes out text file starts, written in above code, first 2 columns mashed together:

n = 665 1777pi- -211 1.19978 0.715507 32.7878 0.13957 1.39694 1779pi+ 211 -8.24173 6.07047 -31.6818 0.13957 10.2361

that first couple lines of output (the programme shows line number particle produced , relevant info name, mass...etc.). cannot seem format don't have utilize inserted spaces set in hand.

as written in above code, first 2 columns mashed together

well, yes, explicitly wrote first 2 columns no whitespace between them:

myfile << left << setw(4) << int(part[j]); myfile << setw(4) << left << pythia.event[part[j]].name() << ...

if want general way format without worrying adding manual whitespace, split 2 steps:

create vector<string> containing columns each line (you can utilize ostringstream format each column individually)

write function take vector , write ostream, spaces between.

std::copy(begin, end, std::ostream_iterator(myfile, " "));

will sufficient if want fixed number of spaces between each column

c++

No comments:

Post a Comment