c++按格式写txt文档

一种是使用fprintf

/* fprintf example */
#include <stdio.h>

int main ()
{
   FILE * pFile;
   int n;
   char name [100];

   pFile = fopen ("myfile.txt","w");
   for (n=0 ; n<3 ; n++)
   {
     puts ("please, enter a name: ");
     gets (name);
     fprintf (pFile, "Name %d [%-10.10s]\n",n+1,name);
   }
   fclose (pFile);

   return 0;
}

result:
Name 1 [John      ] 
Name 2 [Jean-Franc] 
Name 3 [Yoko      ]


tips:
 
 
  • %-10.10s  左对齐(-),最小10个字符,最多10个字符,即长度固定,始终为10
 
/* example 2*/
fprintf(f,"[%s][%4s][%-4s],[%4s][%.4s]","a","a","a","abcde","abcde");//[a][   a][a   ],[abcde][abcd]


另一种方式

一、以操纵子方式格式化

数据输入输出的格式控制使用系统头文件<iomanip>中提供的操纵符。把它们作为插入操作符<<的输出对象即可。如setiosflags、setw、setfill、setprecision、hex、oct等。


(一)、常用的流操纵算子:



(二)、ios类的枚举常量



 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <iomanip>

using  namespace std;

// 通过操纵子方式进行格式化输出
// 宽度控制
// 对齐控制
// 填充控制
// 精度控制
// 进制输出
int main( void)
{
     //system("chcp 936");
     int n =  64;
     double d =  123. 45;
     double d2 =  0. 0187;

    cout <<  "=================宽度控制=====================" << endl;
    cout << n <<  '#' << endl;
    cout << setw( 10) << n <<  '#' << n << endl;       // 宽度控制不会影响下一个输出

    cout <<  "=================对齐控制=====================" << endl;
    cout << setw( 10) << setiosflags(ios::left) << n <<  '#' << endl;
    cout << setw( 10) << n <<  '#' << endl;            // 对齐控制会影响下一个输出
     //cout<<setw(10)<<setiosflags(ios::right)<<n<<'#'<<endl;
    cout << setw( 10) << resetiosflags(ios::left) << n <<  '#' << endl;  //去除左对齐

    cout <<  "=================填充控制=====================" << endl;
    cout << setw( 10) << setfill( '?') << n <<  '#' << endl;
    cout << setw( 10) << n <<  '#' << endl;            // 填充控制会影响下一个输出
    cout << setw( 10) << setfill( ' ') << n <<  '#' << endl;

    cout <<  "=================精度控制=====================" << endl;
    cout << setprecision( 4) << d << endl;  //有效数字
    cout << setprecision( 2) << d2 << endl;

    cout << setiosflags(ios::fixed);
    cout << setprecision( 4) << d << endl;  // 小数点后面位数
    cout << setprecision( 2) << d2 << endl;

    cout <<  "=================进制输出=====================" << endl;

    cout << n << endl;
    cout << oct << n << endl;
    cout << hex << n << endl;
    cout << endl;

    cout << setiosflags(ios::showbase);  //八进制加前缀0,十六进制加前缀0x
    cout << dec << n << endl;
    cout << oct << n << endl;
    cout << hex << n << endl;

    cout << endl;
    cout << setbase( 10) << n << endl;  //八进制加前缀0,十六进制加前缀0x
    cout << setbase( 8) << n << endl;
    cout << setbase( 16) << n << endl;

     return  0;
}



二、以类成员函数方式格式化

通过调用流的成员函数控制格式,如setf、unsetf、width、fill、precision等。优点是在设置格式同时,可以返回以前的设置,便于恢复原来的设置。


ios类提供成员函数对流的状态进行检测和进行输入输出格式控制等操作:




 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
//#include <iomanip>

using  namespace std;

// 通过成员函数方式进行格式化输出
// 宽度控制
// 对齐控制
// 填充控制
// 精度控制
// 进制输出
int main( void)
{
     //system("chcp 936");
     int n =  64;
     double d =  123. 45;
     double d2 =  0. 0187;

    cout <<  "=================宽度控制=====================" << endl;
    cout << n <<  '#' << endl;
    cout.width( 10);
    cout << n <<  '#' << n << endl;                   // 宽度控制不会影响下一个输出

    cout <<  "=================对齐控制=====================" << endl;
    cout.width( 10);
    cout.setf(ios::left);
    cout << n <<  '#' << endl;
    cout.width( 10);
    cout << n <<  '#' << endl;                // 对齐控制会影响下一个输出
     //cout.width(10);
     //cout.setf(ios::right);
     //cout<<n<<'#'<<endl;

    cout.width( 10);
    cout.unsetf(ios::left);
    cout << n <<  '#' << endl;

    cout <<  "=================填充控制=====================" << endl;
    cout.width( 10);
    cout.fill( '?');
    cout << n <<  '#' << endl;

    cout.width( 10);
    cout << n <<  '#' << endl;                // 填充控制会影响下一个输出

    cout.width( 10);
    cout.fill( ' ');
    cout << n <<  '#' << endl;

    cout <<  "=================精度控制=====================" << endl;
    cout.precision( 4);
    cout << d << endl;
    cout.precision( 2);
    cout << d2 << endl;

    cout.setf(ios::fixed);
    cout.precision( 4);
    cout << d << endl;
    cout.precision( 2);
    cout << d2 << endl;;

    cout <<  "=================进制输出=====================" << endl;

    cout.setf(ios::showbase);
    cout << n << endl;
    cout.unsetf(ios::dec);
    cout.setf(ios::oct);
    cout << n << endl;

    cout.unsetf(ios::oct);
    cout.setf(ios::hex);
    cout << n << endl;

     return  0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值