I have the following working code which produces formatted output using an ofstream, however I feel that all of the repeated format specifiers make it hard to read. Is there any way to set the precision, width and alignment permanently for a given stream, which would clean things up nicely?
#include <fstream>
#include <iomanip>
ofstream myfile;
myfile.open(Output.c_str(), ios::trunc);
for (int i = 0; i < iLen; i++) {
myfile <<std::fixed << std::setw( iWidth ) << std::setprecision( iDecimals )<< std::right << pDist[i] << " "
<<std::fixed << std::setw( iWidth ) << std::setprecision( iDecimals )<< std::right << pRes[i] << " "
<<std::fixed << std::setw( iWidth ) << std::setprecision( iDecimals )<< std::right << pPhase[i] << "\n";
}
myfile.close();
std::fixed, std::setprecision, and std::right are all "permanent". They set persistent flags on the stream object.
The only one that you need to put every time is setw, which (as I understand) does not set a flag on the stream object, just adds a certain amount of whitespace by altering the stream itself. But it leaves the stream alone.
This is similar to endl, which operates on the stream but leaves the actual stream object alone.
This answer by #LokiAstari on a similar question details which specifiers are, as they put it, "sticky" (and why).
EDIT: Cleared up distinctions between the stream object and the actual stream
The only manipulator you need to worry about is std::setw as the width is set back to zero whenever operator<< is called. Before your loop, you can set your "permanent" ios flags:
myfile << std::fixed << std::setprecision(iDecimals) << std::right;
for (int i = 0; i < iLen; i++) {
myfile << std::setw( iWidth ) << pDist[i] << " "
<< std::setw( iWidth ) << pRes[i] << " "
<< std::setw( iWidth ) << pPhase[i] << "\n";
}
If you want to unset these flags in one go, you can use:
std::cout.copyfmt(std::ios(NULL));
As stated in this answer.
Related
In a C++ code I have a matrix of double variables which I print out. However because all of them have different number of digits, the output format is destroyed. One solution is to do
cout.precision(5) but I want different columns have a different precision. Also, because there are negative values in some cases, the presence of the - sign also causes problems. How to get around this and produce a properly formatted output?
Off the top of my head, you can use setw(int) to specify the width of the output.
like this:
std::cout << std::setw(5) << 0.2 << std::setw(10) << 123456 << std::endl;
std::cout << std::setw(5) << 0.12 << std::setw(10) << 123456789 << std::endl;
gives this:
0.2 123456
0.12 123456789
The key is, as others have said, to use manipulators. What they
neglected to say is that you normally use manipulators that you write
yourself. An FFmt manipulator (which corresponds to the F format in
Fortran is fairly easy:
class FFmt
{
int myWidth;
int myPrecision;
public:
FFmt( int width, int precision )
: myWidth( width )
, myPrecision( precision )
{
}
friend std::ostream&
operator<<( std::ostream& dest, FFmt const& fmt )
{
dest.setf( std::ios_base::fixed, std::ios_base::formatfield );
dest.precision( myPrecision );
dest.width( myWidth );
return dest;
}
};
This way, you can define a variable for each column, say:
FFmt col1( 8, 2 );
FFmt col2( 6, 3 );
// ...
and write:
std::cout << col1 << value1
<< ' ' << col2 << value2...
In general, except in the simplest programs, you should probably not be
using the standard manipulators, but rather custom manipulators based on
your application; e.g. temperature and pressure if that's the sort of
thing your dealing with. In this way, it's clear in the code what
you're formatting, and if the client suddenly asks for one more digit in
the pressure, you know exactly where to make the change.
Use manipulators.
From sample here:
#include <iostream>
#include <iomanip>
#include <locale>
int main()
{
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << "Left fill:\n" << std::left << std::setfill('*')
<< std::setw(12) << -1.23 << '\n'
<< std::setw(12) << std::hex << std::showbase << 42 << '\n'
<< std::setw(12) << std::put_money(123, true) << "\n\n";
std::cout << "Internal fill:\n" << std::internal
<< std::setw(12) << -1.23 << '\n'
<< std::setw(12) << 42 << '\n'
<< std::setw(12) << std::put_money(123, true) << "\n\n";
std::cout << "Right fill:\n" << std::right
<< std::setw(12) << -1.23 << '\n'
<< std::setw(12) << 42 << '\n'
<< std::setw(12) << std::put_money(123, true) << '\n';
}
Output:
Left fill:
-1.23*******
0x2a********
USD *1.23***
Internal fill:
-*******1.23
0x********2a
USD ****1.23
Right fill:
*******-1.23
********0x2a
***USD *1.23
Take a look at stream manipulators, especially std::setw and std::setfill.
float f = 3.1415926535;
std::cout << std::setprecision(5) // precision of floating point output
<< std::setfill(' ') // character used to fill the column
<< std::setw(20) // width of column
<< f << '\n'; // your number
Try using setw manipulator. Please refer http://www.cplusplus.com/reference/iostream/manipulators/setw/ for further information
There is a way using i/o manipulators, but I find it unwieldy. I would just write a function like this:
template<typename T>
std::string RightAligned(int size, const T & val)
{
std::string x = boost::lexical_cast<std::string>(val);
if (x.size() < size)
x = std::string(size - x.size(), ' ') + x;
return x;
}
I need to print hex and decimal values in my console. I used the following piece of code to do it.
std::stringstream ss;
ss << "0x" << std::uppercase << std::hex << 15;
std::cout << ss.str() << std::endl;
ss.str("");
ss.clear();
ss << 15;
std::cout << ss.str() << std::endl;
But I am getting both values in Hex format. How to reset stringstream?
How to reset stringstream?
Format flags are sticky.
You can save the old format flags to restore them later:
std::stringstream ss;
auto oldFlags = ss.flags(); // <<
ss << "0x" << std::uppercase << std::hex << 15;
std::cout << ss.str() << std::endl;
ss.flags(oldFlags); // <<
ss << 15;
std::cout << ss.str() << std::endl;
Assuming you know which formatting flag are actually used, you can just save their original value and restore them later. When the set of formatting flags being changed is large and possibly somewhat out of the control of the function, there are essentially two approaches which are both, unfortunately, not really very cheap:
Do not use the std::stringstream directly but rather use a temporary stream to which the format flags get applied:
{
std::ostream out(ss.rdbuf());
out << std::showbase << std::uppercase << std::hex << 15;
}
ss << 15;
You can use the copyfmt() member to copy all the formatting flags and later restore these:
std::ostream aux(0); // sadly, something stream-like is needed...
out.copyfmt(ss);
out << std::showbase << std::uppercase << std::hex << 15;
ss.copyfmt(aux);
out << 15;
Both approaches need to create a stream which is, unfortunately, not really fast due to a few essentially mandatory synchronizations (primarily for creating the std::locale member).
Just use
std::nouppercase
ss << std::uppercase;
...
ss << std::nouppercase;
...
I have the following code
cout << setfill('0') << setw(4) << hex << 100 << 100 << std::endl;
The output is:
006464
If I want to let every number with width 4, I have to use
out << setfill('0') << setw(4) << hex << 100 << sew(4) << 100 << std::endl;
But if I want to print every number with hex and setfill('0'), I only need to set setfill('0') and std::hex once.
Does c++ design this on purpose? what is its intention?
Yes it is on purpose. The stream operations are internally peppered with resets of the field width, specified by the standard. I think there's no good answer as to why.
I am working on a C++ program with a lot of numbers that are type double (values in the millions and billions with just a couple places to the right of the decimal point). I am performing calculations on these numbers and then printing the result to text/CSV files. I noticed that in the text files, all of my numbers appear to be rounded (to six digits). So, a value of 13,169,911 is showing up as 13,169,900 in my output file.
Is this rounding only occuring on the print? In order to get the full number of digits in the variable, do I just need to specify something when I write to a file? I included a sample of my write to file code below:
void PrintPropFinance(vector<PropFinance>& PF, int NumProps, int Iterations, int ForecastLength,
string CurDeal, string ModelRunID, string ScenName, Assumptions& Ass) {
string filename;
ofstream OutFile;
ostringstream s1;
s1 << BASEPATH << "Output/" << CurDeal << "_" << ModelRunID << "_" <<
ScenName << "_PropFinance" << ".csv";
filename = s1.str();
OutFile.open(filename);
// Put in the column headers first
OutFile << "PropID" << ","
<< "Item" << ","
<< "StartDate" << ","
<< "NumOfPeriod" << ","
<< "Result" << ","
<< "Isap" << ","
<< "CurLoanBal" << ","
for (int i=1; i<=NumProps; ++i) {
// Populate the single-vector variables
OutFile << PF[i].PropID << ","
<< PF[i].Item << ","
<< PF[i].StartDate << ","
<< PF[i].NumOfPeriod << ","
<< PF[i].Result << ","
<< PF[i].Isap << ","
<< PF[i].CurLoanBal << ","
<< endl;
}
OutFile.close();
}
// Prop finance class definition
class PropFinance {
public:
string PropID;
int Item;
string StartDate;
int NumOfPeriod;
string Isap;
double CurLoanBal;
}
The problem is likely to do with the way the output stream produces the output for doubles: if 13169911 gets printed in "scientific notation", it would look like 1.31699E7. Excel will read this notation just fine, but would put zeros for the digits it does not "see", making the number look like 13,169,900.
To fix this problem, add fixed manipulator when you output your double to ensure that all digits get printed:
OutFile << PF[i].PropID << ","
<< PF[i].Item << ","
<< PF[i].StartDate << ","
<< PF[i].NumOfPeriod << ","
<< fixed << PF[i].Result << ","
<< PF[i].Isap << ","
<< fixed << PF[i].CurLoanBal << ","
<< endl;
You need to use std::setprecision to increase the precision of the stream. By default an iostream has only 6 digits of precision.
Try this:
OutFile << std::setprecision(std::numeric_limits<long double>::digits10 << PF[i].CurLoanBal;
Bear in mind that this will affect all subsequent operations on the stream. To be honest though, that's probably what you want!
As comparison between std::setprecision and std::fixed, this program:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <limits>
int main()
{
const long double test_value = 13169911.7777777;
std::cout << "default precision (6): " << test_value << '\n'
<< "std::fixed: " << std::fixed << test_value << '\n'
<< "std::precision(10): " << std::defaultfloat << std::setprecision(10) << test_value << '\n'
<< "std::precision(10) & std::fixed: " << std::fixed << std::setprecision(10) << test_value << '\n'
<< "max precision: " << std::defaultfloat << std::setprecision(std::numeric_limits<long double>::digits10) << test_value << '\n'
<< "max precision & std::fixed: " << std::fixed << std::setprecision(std::numeric_limits<long double>::digits10) << test_value << '\n'
;
}
Produces this output:
default precision (6): 1.31699e+007
std::fixed: 13169911.777778
std::precision(10): 13169911.78
std::precision(10) & std::fixed: 13169911.7777777000
max precision: 13169911.7777777
max precision & std::fixed: 13169911.777777700000000
So I think you may want std::setprecision rather than std::fixed. Though I imagine that you'll only have two decimal places anyway so perhaps it doesn't matter.
Read more here: http://en.cppreference.com/w/cpp/io/manip/setprecision
In a C++ code I have a matrix of double variables which I print out. However because all of them have different number of digits, the output format is destroyed. One solution is to do
cout.precision(5) but I want different columns have a different precision. Also, because there are negative values in some cases, the presence of the - sign also causes problems. How to get around this and produce a properly formatted output?
Off the top of my head, you can use setw(int) to specify the width of the output.
like this:
std::cout << std::setw(5) << 0.2 << std::setw(10) << 123456 << std::endl;
std::cout << std::setw(5) << 0.12 << std::setw(10) << 123456789 << std::endl;
gives this:
0.2 123456
0.12 123456789
The key is, as others have said, to use manipulators. What they
neglected to say is that you normally use manipulators that you write
yourself. An FFmt manipulator (which corresponds to the F format in
Fortran is fairly easy:
class FFmt
{
int myWidth;
int myPrecision;
public:
FFmt( int width, int precision )
: myWidth( width )
, myPrecision( precision )
{
}
friend std::ostream&
operator<<( std::ostream& dest, FFmt const& fmt )
{
dest.setf( std::ios_base::fixed, std::ios_base::formatfield );
dest.precision( myPrecision );
dest.width( myWidth );
return dest;
}
};
This way, you can define a variable for each column, say:
FFmt col1( 8, 2 );
FFmt col2( 6, 3 );
// ...
and write:
std::cout << col1 << value1
<< ' ' << col2 << value2...
In general, except in the simplest programs, you should probably not be
using the standard manipulators, but rather custom manipulators based on
your application; e.g. temperature and pressure if that's the sort of
thing your dealing with. In this way, it's clear in the code what
you're formatting, and if the client suddenly asks for one more digit in
the pressure, you know exactly where to make the change.
Use manipulators.
From sample here:
#include <iostream>
#include <iomanip>
#include <locale>
int main()
{
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << "Left fill:\n" << std::left << std::setfill('*')
<< std::setw(12) << -1.23 << '\n'
<< std::setw(12) << std::hex << std::showbase << 42 << '\n'
<< std::setw(12) << std::put_money(123, true) << "\n\n";
std::cout << "Internal fill:\n" << std::internal
<< std::setw(12) << -1.23 << '\n'
<< std::setw(12) << 42 << '\n'
<< std::setw(12) << std::put_money(123, true) << "\n\n";
std::cout << "Right fill:\n" << std::right
<< std::setw(12) << -1.23 << '\n'
<< std::setw(12) << 42 << '\n'
<< std::setw(12) << std::put_money(123, true) << '\n';
}
Output:
Left fill:
-1.23*******
0x2a********
USD *1.23***
Internal fill:
-*******1.23
0x********2a
USD ****1.23
Right fill:
*******-1.23
********0x2a
***USD *1.23
Take a look at stream manipulators, especially std::setw and std::setfill.
float f = 3.1415926535;
std::cout << std::setprecision(5) // precision of floating point output
<< std::setfill(' ') // character used to fill the column
<< std::setw(20) // width of column
<< f << '\n'; // your number
Try using setw manipulator. Please refer http://www.cplusplus.com/reference/iostream/manipulators/setw/ for further information
There is a way using i/o manipulators, but I find it unwieldy. I would just write a function like this:
template<typename T>
std::string RightAligned(int size, const T & val)
{
std::string x = boost::lexical_cast<std::string>(val);
if (x.size() < size)
x = std::string(size - x.size(), ' ') + x;
return x;
}