0

I know there are tons of posts and websites describing that the best way of formatting the output of (f)streams with c++. I am fixing the representation of numbers and requesting columns of 9 spaces to be filled with number with a max number of 5 digits is I understood the idea correctly. However I don't understand why my code is not working as expected...

    outfile.open(outfilename.str()); /* WRITING TO FILE */
    outfile.width(9);
    outfile.precision(5);
    outfile.setf(ios_base::fixed,ios_base::floatfield);
    outfile << "#   E_Fermi (eV) : " << efermi << "   Number of points in energy grid : " << nen << "   Smearing HWHM (eV): " << hwhm << "   Units: rho*Ang^3/eV " << endl;
    outfile << "#Energy\tTot\ts tot\tp tot\td tot\tp_x\tp_y\tp_z\td_xy\td_yz\td_z2\td_xz\td_x2-y2" << endl;
    for (int e=0;e<nen;e++) 
    {
        outfile << plote[e] << "\t" << plotdostot[e] << "\t" << plotdoss[e] << "\t" << plotdosp[e] << "\t" << plotdosd[e];
        outfile << "\t" << plotdospx[e] << "\t" << plotdospy[e] << "\t" << plotdospz[e];
        outfile << "\t" << plotdosdxy[e] << "\t" << plotdosdyz[e] << "\t" << plotdosdz2[e] << "\t" << plotdosdxz[e] << "\t" << plotdosdx2y2[e] << endl;
    }
    outfile.close();

This is an example of the output: as you may notice the header is not aligned with the data and at a certain point even the data columns are shifted.

#   E_Fermi (eV) : 0.00000   Number of points in energy grid : 2000   Smearing HWHM (eV): 0.05000   Units: rho*Ang^3/eV 
#Energy Tot     s tot   p tot   d tot   p_x     p_y     p_z     d_xy    d_yz    d_z2    d_xz    d_x2-y2
-18.12233       79.52857        79.49073        0.00000 0.00000 0.00000 0.00106 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.11480       92.10762        92.06144        0.00000 0.00000 0.00000 0.00160 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.10727       105.32436       105.26909       0.00000 0.00000 0.00000 0.00233 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.09974       119.03944       118.97447       0.00000 0.00000 0.00000 0.00330 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.09221       133.10164       133.02643       0.00000 0.00000 0.00000 0.00452 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.08468       147.34603       147.26015       0.00000 0.00000 0.00000 0.00600 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.07715       161.59221       161.49530       0.00000 0.00000 0.00000 0.00772 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.06962       175.64351       175.53523       0.00000 0.00000 0.00000 0.00963 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.06209       189.28817       189.16828       0.00000 0.00000 0.00000 0.01163 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.05455       202.30318       202.17157       0.00000 0.00000 0.00000 0.01362 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.04702       214.46109       214.31783       0.00000 0.00000 0.00000 0.01546 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.03949       225.53974       225.38519       0.00000 0.00000 0.00000 0.01700 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.03196       235.33477       235.16965       0.00000 0.00000 0.00000 0.01811 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.02443       243.67361       243.49907       0.00000 0.00000 0.00000 0.01870 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.01690       250.42936       250.24698       0.00000 0.00000 0.00000 0.01871 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.00937       255.53174       255.34346       0.00000 0.00000 0.00000 0.01815 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-18.00184       258.97236       258.78042       0.00000 0.00000 0.00000 0.01705 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.99431       260.80172       260.60850       0.00000 0.00000 0.00000 0.01553 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.98677       261.11692       260.92478       0.00000 0.00000 0.00000 0.01371 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.97924       260.04102       259.85215       0.00000 0.00000 0.00000 0.01172 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.97171       257.69749       257.51381       0.00000 0.00000 0.00000 0.00971 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.96418       254.18505       254.00812       0.00000 0.00000 0.00000 0.00780 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.95665       249.55871       249.38971       0.00000 0.00000 0.00000 0.00607 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.94912       243.82226       243.66200       0.00000 0.00000 0.00000 0.00458 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.94159       236.93464       236.78365       0.00000 0.00000 0.00000 0.00335 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.93406       228.82972       228.68827       0.00000 0.00000 0.00000 0.00237 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.92652       219.44521       219.31341       0.00000 0.00000 0.00000 0.00163 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.91899       208.75444       208.63231       0.00000 0.00000 0.00000 0.00108 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.91146       196.79376       196.68125       0.00000 0.00000 0.00000 0.00070 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.90393       183.67949       183.57652       0.00000 0.00000 0.00000 0.00044 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.89640       169.61081       169.51729       0.00000 0.00000 0.00000 0.00026 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.88887       154.85852       154.77431       0.00000 0.00000 0.00000 0.00015 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.88134       139.74218       139.66711       0.00000 0.00000 0.00000 0.00009 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.87381       124.60092       124.53475       0.00000 0.00000 0.00000 0.00005 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.86628       109.76345       109.70587       0.00000 0.00000 0.00000 0.00003 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.85874       95.52238        95.47300        0.00000 0.00000 0.00000 0.00001 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.85121       82.11651        82.07482        0.00000 0.00000 0.00000 0.00001 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.84368       69.72218        69.68759        0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.83615       58.45312        58.42496        0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.82862       48.36669        48.34422        0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.82109       39.47353        39.45598        0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.81356       31.74811        31.73472        0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.80603       25.13827        25.12829        0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.79850       19.57265        19.56539        0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.79096       14.96618        14.96103        0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.78343       11.22410        11.22055        0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.77590       8.24530 8.24291 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
-17.76837       5.92557 5.92402 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000

Can anyone suggest what's happening here? Thanks in advance,

Aldo

Aldo_U
  • 5
  • 6
  • 1
    Don't use tabs. Only use `setw` manipulator. – Some programmer dude Apr 13 '18 at 20:22
  • 1
    Don't use tabs for columns. [This](https://stackoverflow.com/questions/14765155/how-can-i-easily-format-my-data-table-in-c) is the standard way to do it – NathanOliver Apr 13 '18 at 20:22
  • Ok for tabs, but I am already setting the width with outfile.width(9). The solution I usually find, as @NathanOliver reported, require to set the properties for every field, which is something I would like to avoid (just because it is too long) – Aldo_U Apr 13 '18 at 20:42
  • 1
    If you want to use C++ streams it is the way you have to do it. You could probably write a function to hide it but you'll have to write the code at least once somewhere. – NathanOliver Apr 13 '18 at 20:46
  • So basically the .width() and .precision() methods are not doing anything? – Aldo_U Apr 13 '18 at 20:56
  • Pretty much. They fail on you when you have some data that is smaller then the rest – NathanOliver Apr 13 '18 at 20:59

0 Answers0