Tuesday, May 12, 2009

C++ I/O Tips 2 Input/Output

Tips and tricks for using C++ I/O (input/output)
(http://www.augustcouncil.com/~tgibson/tutorial/iotips.html)

How to prepare the output stream to print fixed precision numbers (3.40 instead of 3.4)
  std::cout.setf(ios::fixed, ios::floatfield);
std::cout.setf(ios::showpoint);
std::cout.precision(2);

How to set the width of a printing field
  Given: int one=4, two=44;
std::cout << one << std::endl.;
//output: "4"

std::cout << setw(2) << one << std::endl.;
//output: " 4"

std::cout.fill('X');
std::cout << setw(2) << one << std::endl.;
//output: "X4"

std::cout.fill('X');
std::cout << setw(2) << two << std::endl.;
//output: "44"

No comments:

Post a Comment