博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++输出文件流ofstream用法详解
阅读量:4096 次
发布时间:2019-05-25

本文共 4436 字,大约阅读时间需要 14 分钟。

目录

这里写图片描述

头文件 <fstream>包含的多个文件流类,这里列出常用的4个:

  • ifstream Input file stream class (class )
  • ofstream Output file stream (class )
  • fstream Input/output file stream class (class )
  • filebuf File stream buffer (class )

一. 输入流 用法

输入流的继承关系:

ios_base <- ios <- ostream <- ofstream

Public member functions (1-6)

1, (constructor)

default (1) ofstream(); // 只定义不关联initialization (2)  //关联文件filename,默认模式 ios_base::outexplicit ofstream (const char* filename, ios_base::openmode mode = ios_base::out);explicit ofstream (const string& filename, ios_base::openmode mode = ios_base::out);copy (3)    //禁止拷贝赋值ofstream (const ofstream&) = delete;move (4)    //可以右值引用临时变量赋值ofstream (ofstream&& x);

2, ofstream::open

//和第二种构造函数一样,绑定文件void open (const   char* filename,  ios_base::openmode mode = ios_base::out);void open (const string& filename,  ios_base::openmode mode = ios_base::out);

3, ofstream::is_open

bool is_open() const;// 文件打开返回 true ,否则 false

4, ofstream::close

void close();// Closes the file currently associated with the object, disassociating it from the stream.

5, ofstream::rdbuf

filebuf* rdbuf() const;// 返回一个指针,指向 filebuf 对象,filebuf 要么通过open()和文件绑定,要么通过rdbuf()与fstream对象绑定,绑定后才能使用。int main () {  std::ifstream ifs ("test.txt");  std::ofstream ofs ("copy.txt");  std::filebuf* inbuf  = ifs.rdbuf();  std::filebuf* outbuf = ofs.rdbuf();  char c = inbuf->sbumpc();  while (c != EOF)  {    outbuf->sputc (c);    c = inbuf->sbumpc();  }  ofs.close();  ifs.close();  return 0;}

6,ofstream::operator=

copy (1) //禁止copy赋值ofstream& operator= (const ofstream&) = delete;move (2) // 可以右值引用创建对象。ofstream& operator= (ofstream&& rhs);

Public member functions inherited from ostream (7-11)

7,std::ostream::operator<<

用法和 cout<< 一样,是写数据到文件最方便的函数,重载了常用的数据类型。

arithmetic types (1)   ostream& operator<< (bool val);ostream& operator<< (short val);ostream& operator<< (unsigned short val);ostream& operator<< (int val);ostream& operator<< (unsigned int val);ostream& operator<< (long val);ostream& operator<< (unsigned long val);ostream& operator<< (long long val);ostream& operator<< (unsigned long long val);ostream& operator<< (float val);ostream& operator<< (double val);ostream& operator<< (long double val);ostream& operator<< (void* val);stream buffers (2)  ostream& operator<< (streambuf* sb );manipulators (3)    ostream& operator<< (ostream& (*pf)(ostream&));ostream& operator<< (ios& (*pf)(ios&));ostream& operator<< (ios_base& (*pf)(ios_base&));

string数据类型,作为参数写入到ofstream,在头文件<string>

中对输出运算符重载。
std::operator<< (string)

ostream& operator<< (ostream& os, const string& str);

8,ostream::put

ostream& put (char c);//插入字符 c 到流中

9,ostream::write

ostream& write (const char* s, streamsize n);//从数组s中取n 个字符插入到流中

10,ostream::tellp

streampos tellp();返回文件指针的位置, streampos 可以转为int

11,ostream::seekp

(1) ostream& seekp (streampos pos);(2) ostream& seekp (streamoff off, ios_base::seekdir way);//Sets the position where the next character is to be inserted into the output stream.

参数 pos 是流中的绝对位置可以转化为 int

参数 off 是偏移量,与way相关,类型是 int
参数 way 可以选下表中的任意一个常量。

value offset is relative to…
ios_base::beg beginning of the stream
ios_base::cur current position in the stream
ios_base::end end of the stream

Public member functions inherited from ios(12-14)

12,ios::good

bool good() const;bool eof() const;bool fail() const;bool bad() const;

检测流的状态是否正常。当错误的状态flags (eofbit, failbit and badbit) 都没被设置的时候返回true

特定的错误状态可以用下面的函数(eof, fail, and bad)来检测。

iostate value (member constant) indicates good() eof() fail() bad() rdstate()
goodbit No errors (zero value iostate) true false false false goodbit
eofbit End-of-File reached on input operation false true false false eofbit
failbit Logical error on i/o operation false false true false failbit
badbit Read/writing error on i/o operation false false true true badbit

13,ios::operator!

bool operator!() const;//Returns true if either failbit or badbit is set, and false otherwise.// 有错误状态返回 true// evaluating a stream (not)#include 
// std::cout#include
// std::ifstreamint main () { std::ifstream is; is.open ("test.txt"); if (!is) std::cerr << "Error opening 'test.txt'\n"; return 0;}

14,ios::operator bool

explicit operator bool() const;C++11: Return true if none of failbit or badbit is set. false otherwise.// 在条件语句中,无错误返回真,有错返回假。int main () {  std::ifstream is;  is.open ("test.txt");  if (is) {    // read file  }  else {    std::cerr << "Error opening 'test.txt'\n";  }  return 0;}
你可能感兴趣的文章
一篇文章带你深入了解MySQL 索引相关
查看>>
GitHub上标星高达55.3Kstar的牛逼项目,附项目源代码
查看>>
你要连MySQL事务实现的基本原理都不懂,那你的面试基本凉凉
查看>>
到了2020年,技术水平到底需要达到怎样的程度才能成为顶级的阿里P8架构师
查看>>
牛皮了!一篇文章直接解决关于TCP的23种疑难问题!
查看>>
使用Docker、Nginx和Jenkins实现前端自动化部署
查看>>
牛皮了!2020最全MySQL索引优化架构+索引系统+数据结构选择+红黑树+B树
查看>>
大数据简介,技术体系分类整理
查看>>
Spring Boot 2.x:Elasticsearch快速入门
查看>>
鸿蒙OS被知乎嘲讽 程序员大佬澄清:华为对Linux贡献数一数二
查看>>
NIO框架详解:Netty的高性能之道(建议收藏)
查看>>
面试阿里、京东、字节跳动90%会被问到这些常用的Java技术
查看>>
java内存溢出问题分析过程
查看>>
大数据从业人员必要技能之Kafka
查看>>
还在担心服务挂掉?Sentinel Go 让服务稳如磐石
查看>>
Java ConcurrentHashMap 高并发安全实现原理解析
查看>>
珍贵经验!Spring Boot 内存又泄露,排查太难了!
查看>>
Java如何支持函数式编程?
查看>>
凭借这份GitHub上疯狂霸榜标星140k的Spring全家桶文档,我接连斩获阿里、京东、腾讯三家offer
查看>>
2020年8月程序员平均工资最新出炉,你拖后腿了么?没关系,一文帮你补起来!
查看>>