printf & fprintf & sprintf & swprintf_s

1.printf

  • double
double dbValue = 56789.1234;
printf("dbValue = %.2f\n", dbValue);
  • INT64/UINT64

typedef signed __int64 INT64;
typedef unsigned __int64 UINT64;

INT64  nTimestamp = 1234567890123;
printf("nTimestamp = %I64d\n", nTimestamp);

UINT64 unTimestamp = 12345678901234567;
printf("unTimestamp = %I64u\n", unTimestamp);
运行结果

2. fprintf

  • 写文件
        FILE *fp = fopen(".\\audioTimestamp.txt", "at");
        double dbTimestamp = 12345.67;
        fprintf(fp, "%.2f\n", dbTimestamp);
        fflush(fp);
        fclose(fp);

fopen 的参数:
'a': 以附加的方式打开只写文件。
若文件不存在,则会建立该文件.
如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留.
'at' : 追加打开一个文本文件,并在文件末尾写数据

3.sprintf

    int  nYear                 = 2016;
    char szBuffer[128]         = "Hello World!";
    char szMessageBuffer[1024] = {0};
    sprintf(szMessageBuffer, "%s Welcome to the year %d\n", szBuffer, nYear);
    cout<
sprintf运行结果

4. swprintf_s

swprintf_s returns the number of wide characters stored in buffer, not counting the terminating null wide character.

    wchar_t wszBuffer[1024] = {0};
    int nLen = swprintf_s(wszBuffer, 1024, L"%s", "北京110");
    wprintf(L"Buffer:%S; length:%d", wszBuffer, nLen);
swprintf_s运行结果

你可能感兴趣的:(printf & fprintf & sprintf & swprintf_s)