写文件

1. 概述

写文件是 MFC 应用程序最基本的功能,本文记录了如何使用 fstream 来写文件,并用 ios 指定打开文件的方式。

2. 头文件

#include "fstream.h"

3.可重用代码

fstream _Out("data.txt",ios::out);
_Out << "Data1 = " << Data1 << endl;
...
_Out.close();

4. 说明

关于ios

ios 指定打开文件的方式,有如下几种。如果需要指定多种方式,使用位运算符“|”来组合它们,例如 ios::out|ios::app。

  • ios::app 打开文件,并在文件结尾追加数据.
  • ios::ate Opens an existing file (either input or output) and seeks the end. 
  • ios::in Opens an input file. Use ios::in as an open_mode for an ofstream file to prevent truncating an existing file. 
  • ios::out Opens an output file. When you use ios::out for an ofstream object without ios::app, ios::ate, or ios::in, ios::trunc is implied.
  • ios::nocreate 只能打开一个已有的文件,否则会出错.
  • ios::noreplace 只能打开一个不存在的文件,否则会出错(?).
  • ios::trunc 打开一个文件并替换掉它,如果它已有的话(?).
  • ios::binary 以二进制模式打开文件(默认为文本模式).
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值