C++ 简单写入文件

#include <iostream>

#include <fstream>  ///<文件I/O

using namespace std;

 

int main()

{

       char sAuthor[20];

       int nAge;

       double nPay;

 

       ofstream ioOutFile; ///<定义IO输出对象

       ioOutFile.open("authorInfo.txt");

 

       cout<<"Enter Your Name:";

       cin.getline(sAuthor,20);

       cout<<"Enter Your Age:";

       cin>>nAge;

       cout<<"Enter Your Month Pay:";

       cin>>nPay;

 

       ioOutFile<<fixed;

       ioOutFile.precision(2);

       ioOutFile.setf(ios_base::showpoint);

       ///<以上三句用来设置输出精度为小数点后两位

       ioOutFile<<"Author Name is "<<sAuthor<<endl;

       ioOutFile<<"Author Age is "<<nAge<<endl;

       ioOutFile<<"Author`s Month Pay is "<<nPay<<endl;

 

       ioOutFile.close();

       system("pause");

       return 0;

}

 

/**注意,使用完一个文件文件后,应该将其关闭

       open和close应该成对使用

       如果在程序运行之前文件不存在,方法open()将创建一个新的文件。但是,如果再次运行该程序,

       默认情况下,open()将首先截断该文件,即将其长度截短到零——丢弃原有的内容,

       然后将新的输出加入到该文件中。

       警告:打开已有文件,默认长度截短为零——丢其原有的内容。

**/

转载于:https://my.oschina.net/imtoby/blog/64671

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值