I am curious as to why people eschew printf/sprintf in favor of cout, ios::, etc. Printf is probably the best function in all of the libraries as it offers so much flexibility and power with so little code. For example, if I wanted to put the current time in the format "00:00:00" (hh:mm:ss):
time_t curr;
tm local;
time(&curr);
local=*(localtime(&curr)); // dereference and assign
With cout
cout.width(2); cout.fill('0');
cout << local.tm_hour << ":";
cout.width(2); cout.fill('0');
cout << local.tm_min << ":";
cout.width(2); cout.fill('0');
cout << local.tm_sec << " ";
cout.width(0);
Why all that work when you can just do
With printf
printf("%02d:%02d:%02d", local.tm_hour, local.tm_min, local.tm_sec);
(Yes, I have seen several examples of people doing it the hard way.)
time_t curr;
tm local;
time(&curr);
local=*(localtime(&curr)); // dereference and assign
With cout
cout.width(2); cout.fill('0');
cout << local.tm_hour << ":";
cout.width(2); cout.fill('0');
cout << local.tm_min << ":";
cout.width(2); cout.fill('0');
cout << local.tm_sec << " ";
cout.width(0);
Why all that work when you can just do
With printf
printf("%02d:%02d:%02d", local.tm_hour, local.tm_min, local.tm_sec);
(Yes, I have seen several examples of people doing it the hard way.)