c++输出文件流ofstream用法详解

article/2025/10/19 8:20:44

目录

    • 一. 输入流 ofstream 用法
      • Public member functions (1-6)
        • 1, (constructor)
        • 2, ofstream::open
        • 3, ofstream::is_open
        • 4, ofstream::close
        • 5, ofstream::rdbuf
        • 6,ofstream::operator=
      • Public member functions inherited from ostream (7-11)
        • 7,std::ostream::operator<<
        • 8,ostream::put
        • 9,ostream::write
        • 10,ostream::tellp
        • 11,ostream::seekp
      • Public member functions inherited from ios(12-14)
        • 12,ios::good
        • 13,ios::operator!
        • 14,ios::operator bool

这里写图片描述

头文件 <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 )链接

一. 输入流 ofstream 用法

输入流的继承关系:

ios_base <- ios <- ostream <- ofstream

Public member functions (1-6)

1, (constructor)

default (1) ofstream(); // 只定义不关联initialization (2)  //关联文件filename,默认模式 ios_base::out
explicit 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 可以选下表中的任意一个常量。

valueoffset is relative to…
ios_base::begbeginning of the stream
ios_base::curcurrent position in the stream
ios_base::endend 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)indicatesgood()eof()fail()bad()rdstate()
goodbitNo errors (zero value iostate)truefalsefalsefalsegoodbit
eofbitEnd-of-File reached on input operationfalsetruefalsefalseeofbit
failbitLogical error on i/o operationfalsefalsetruefalsefailbit
badbitRead/writing error on i/o operationfalsefalsetruetruebadbit

13,ios::operator!

bool operator!() const;
//Returns true if either failbit or badbit is set, and false otherwise.
// 有错误状态返回 true// evaluating a stream (not)
#include <iostream>     // std::cout
#include <fstream>      // 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;
}

http://chatgpt.dhexx.cn/article/qcAl87FP.shtml

相关文章

C++文件和流

C文件和流 到目前为止&#xff0c;我们已经使用了 iostream 标准库&#xff0c;它提供了 cin 和 cout 方法分别用于从标准输入读取流和向标准输出写入流。 本教程介绍如何从文件读取流和向文件写入流。这就需要用到 C 中另一个标准库 fstream&#xff0c;它定义了三个新的数据…

什么是https

HTTPS&#xff08;全称&#xff1a;Hypertext Transfer Protocol over Secure Socket Layer&#xff09;&#xff0c;是以安全为目标的HTTP通道&#xff0c;简单讲是HTTP的安全版。即HTTP下加入SSL层&#xff0c;HTTPS的安全基础是SSL&#xff0c;因此加密的详细内容请看SSL。 …

http和https有什么区别 端口号多少

HTTP和HTTPS的基本概念 HTTP&#xff1a;超文本传输协议&#xff0c;是在互联网上应用最广泛的一种网络协议。是一个客户端和服务端请求和应答的标准&#xff08;TCP&#xff09;&#xff0c;用于从WWW&#xff08;超文本&#xff09;服务器传输超文本到本地浏览器的传输协议。…

https 请求的端口是443 注意

注意: 这里录制https的请求 端口号一定是443 才可以抓取到!!!!!! &#xff08;进坑多次&#xff09; 转载于:https://www.cnblogs.com/kaibindirver/p/9223595.html

IP地址,开放端口,http与https的区别

文章目录 一、IP地址的概述二、IP地址分类1、**共有地址**2、 **私有地址** 三、IPV4和V6四、子网掩码、网关、DNS1、 子网掩码2、网关3、DNS服务器 五、获取目标IP地址的方法1、 通过ping命令&#xff1a;2、 通过NSLOOKUP命令&#xff1a;1.使用资源监视器查看&#xff1a;2.…

Linux网络——图解HTTPS协议与端口号认识

Linux网络——图解HTTPS协议与端口号认识 一、确保HTTP安全的方式1.1 HTTP明文加密<1> 通信加密<2> 内容加密 1.2 验证通信方身份1.3 验证报文完整性&#xff0c;防止被篡改 二、HTTP加密认证完整性保护HTTPS2.1 SSL/TLS2.2 对称加密2.3 非对称加密2.4 混合加密 三…

什么是SSL端口?HTTPS配置技术指南

安全套接字层&#xff08;SSL&#xff09;是负责互联网连接的数据身份验证和加密的技术。它加密在两个系统之间&#xff08;通常在服务器和客户端之间&#xff09;之间通过互联网发送的数据&#xff0c;使其保持私密。随着在线隐私的重要性日益增加&#xff0c;您应该熟悉SSL端…

--端口--

目录 一、端口的读写 二、shl和shr指令 我们在之前所讲过&#xff0c;各种存储器都和CPU的地址线、数据线、控制线相连。CPU在操控它们的时候&#xff0c;把它们当做内存来看待&#xff0c;把它们总地看做一个由若干存储单元组成的逻辑存储器&#xff0c;这个逻辑存储器我们称…

服务器中如何检查端口是否开放

服务器中如何检查端口是否开放 端口对于一台服务器来说是至关重要的&#xff0c;它是服务器与外部网络设备的协议出口&#xff0c;它一共拥有65536个(0-65535)&#xff0c;其中一些端口已经是约定好什么协议在使用了的&#xff0c;像80端口就是web服务使用、3389端口是Windows远…

部署https证书的端口是什么意思

端口号能够觉得是机器设备与外部通信沟通交流的出入口。http的端口号为80&#xff0c;那么&#xff0c;你知不知道https证书的端口是什么吗&#xff1f;今日小编就来详细介绍下。 https证书的端口是什么 https证书布署安装&#xff0c;网络服务器必须关联端口号&#xff0c;一…

一分钟了解HTTP和HTTPS协议

很多人存在这样的疑惑就是http与https的区别&#xff0c;这篇文章就跟大家介绍一下。一句话总结HTTPS是身披SSL外壳的HTTP&#xff0c;HTTPS更安全&#xff0c;实际使用中绝大多数的网站现在都采用的是HTTPS协议&#xff0c;这也是未来互联网发展的趋势。 什么是协议&#xff1…

MVC5项目发布到IIS

1.右击项目&#xff0c;点击发布&#xff08;本人用的是vs2017&#xff09; 2 选择要发布的目标位置&#xff0c;简单说就是你想把发布生成的文件保存到哪里。本人在F盘建了个MVCPublishToIIS的文件夹。 发布方法选择文件系统(File System) 3 点击下一页继续 配置选择Release 发…

创建ASP.NET MVC5 应用程序

创建第一个ASP.NET MVC项目。使用Visual Studio 2017/2019创建ASP.NET MVC5应用程序&#xff0c;要求如下&#xff1a; &#xff08;1&#xff09;选择“MVC”模板&#xff0c;创建ASP.NET MVC应用程序。 &#xff08;2&#xff09;分别修改主页、关于我们及联系方式页面内容。…

[Asp.Net MVC5](一)- 理解MVC模式

1. MVC模式概念 MVC模式&#xff08;Model-View-Controller&#xff09;是软件工程中的一种软件架构模式&#xff0c;把软件系统分为以下三个基本部分&#xff1a; ◆Model封装了你的应用数据、应用流程和业务逻辑。“模型”有对数据直接访问的权力&#xff0c;例如对数据库的…

经验之谈 ---- ASP.NET应用程序MVC5模式下的简单实例项目

刚开始做ASP.NET应用程序的时候&#xff0c;自己一脸蒙B&#xff0c;具体的程序流程都不懂&#xff0c;所以自己打算写一个最简单的项目来看看ASP.NET MVC项目的具体流程。 若有写得不好的&#xff0c;还望指出. 目录结构如下图所示&#xff1a; 在企业中开发的时候需要自建A…

MVC5+EF6 入门完整教程五

上篇文章介绍了EF实现CRUD及一些基本的Html Helpers. 这次我们将会对之前的内容进行一些修改和重构&#xff1a; 引入Bootstrap样式&#xff0c;搭建几类共用的模板页&#xff0c;对UI进行一些改造 分类介绍Html Helpers 完善一些功能 文章提纲 理论基础 UI改造详细步骤 总结…

ASP.NET + MVC5 入门完整教程三 (下) ---MVC 松耦合

建立松耦合组件 MVC 模式最重要的特性之一视他支持关注分离&#xff0c;希望应用程序中的组件尽可能独立&#xff0c;只有很少的几个可控依赖项。在理想的情况下&#xff0c;每个组件都不了解其他组件&#xff0c;而只是通过抽象接口来处理应用程序的其他区域&#xff0c;这就…

MVC2 ,MVC3 ,MVC4,MVC5的区别

2010年發行ASP.NET MVC 2.0版&#xff0c;2011年發行ASP.NET MVC 3.0版&#xff0c;2012年發行ASP.NET MVC 4.0版 MVC3 需要.net framework 4.0 版本. 支持多视图引擎 在 ASP.NET MVC3 中&#xff0c;增加视图的对话框中允许你选择你希望的视图引擎&#xff0c;在新建项目…

MVC5+EF6 入门完整教程四

上篇文章主要讲了如何配置EF, 我们回顾下主要过程&#xff1a; 创建Data Model 创建Database Context 创建databaseInitializer配置entityFramework的context配置节。 对这个过程还有疑问的可以去上篇再看一下。 本次我们就主要讲解 (1) EF基本的CRUD (2) 涉及到的常用…

ASP.NET MVC 5 - 入门

注︰本教程的更新的版本是可用在这里使用视觉工作室 2015年。新的教程使用ASP.NET MVC 6 核心&#xff0c;其中在本教程中提供了许多改进。 本教程将教你基本的构建 ASP.NET MVC 5 web 应用程序使用Visual Studio 2013. 下载已完成项目. 本教程由斯科特 格思里(twitterscottgu…