[C++] 用于格式化输出的代码

Sample code 如下,使用setw()函数可以设置输出格式。

#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    ofstream m_osDest;
    string sDest("d:\\log.txt");
    m_osDest.open(sDest);
    if (!m_osDest) // cannot open file
    {
        // error handling
    }
    char cLowSpec[10] = "0.00";
    string m_sTestItem("Item");
    // setw is declared in <iomanip>
    m_osDest << left << setw(36) << m_sTestItem << setw(10) << cLowSpec ;
}

猜你喜欢

转载自blog.csdn.net/ftell/article/details/80481548