c++11 标准模板(STL)(std::basic_ifstream)(四)

news2024/10/6 16:26:39

定义于头文件 <fstream>

template<

    class CharT,
    class Traits = std::char_traits<CharT>

> class basic_ifstream : public std::basic_istream<CharT, Traits>

 类模板 basic_ifstream 实现文件流上的高层输入操作。它将 std::basic_istream 的高层接口赋予基于文件的流缓冲( std::basic_filebuf )。

std::basic_ifstream 的典型实现仅保有一个非导出数据成员: std::basic_filebuf<CharT, Traits> 的实例。

亦定义二个对于常用字符类型的特化:

类型定义
ifstreambasic_ifstream<char>
wifstreambasic_ifstream<wchar_t>

文件操作

检查流是否有关联文件

std::basic_ifstream<CharT,Traits>::is_open

bool is_open();

(C++11 前)

bool is_open() const;

(C++11 起)

 检查文件流是否有关联文件。

等效地调用 rdbuf()->is_open() 。

参数

(无)

返回值

若文件流有关联文件,则为 true ,否则为 false 。

调用示例

#include <fstream>
#include <utility>
#include <string>
#include <iostream>

int main()
{
    std::ifstream ifstream1("test1.txt", std::ios::in);
    std::ifstream ifstream2("test2.txt", std::ios::in);
    std::ifstream ifstream3("test3.txt", std::ios::in);
    std::cout << "ifstream1 is: " << (ifstream1.is_open() ? "true" : "false") << std::endl;
    std::cout << "ifstream2 is: " << (ifstream2.is_open() ? "true" : "false") << std::endl;
    std::cout << "ifstream3 is: " << (ifstream3.is_open() ? "true" : "false") << std::endl;

    std::cout << std::endl;
    return 0;
}

输出

打开文件,并将它与流关联

std::basic_ifstream<CharT,Traits>::open

void open( const char *filename,
           ios_base::openmode mode = ios_base::in );

(1)

void open( const std::filesystem::path::value_type *filename,
           ios_base::openmode mode = ios_base::in );

(2)(C++17 起)

void open( const std::string &filename,                                  
           ios_base::openmode mode = ios_base::in );

(3)(C++11 起)

void open( const std::filesystem::path &filename,                                  
           ios_base::openmode mode = ios_base::in );

(4)(C++17 起)

 将名为 filename 的文件打开并与文件流关联。

失败时调用 setstate(failbit) 。

成功时调用 clear() 。(C++11 起)

1-2) 等效地调用 rdbuf()->open(filename, mode | ios_base::in). (该调用效果上的细节见 std::basic_filebuf::open )。仅若 std::filesystem::path::value_type 非 char 才提供重载 (2) 。 (C++17 起)

3-4) 等效地调用 (1-2) ,如同以 open(filename.c_str(), mode) 。

参数

filename-要打开的文件名
mode-指定打开模式。它是位掩码类型,定义下列常量:
常量解释
app每次写入前寻位到流结尾
binary以二进制模式打开
in为读打开
out为写打开
trunc在打开时舍弃流的内容
ate打开后立即寻位到流结尾

返回值

(无)

调用示例

#include <fstream>
#include <utility>
#include <string>
#include <iostream>

int main()
{
    std::string strFileName1 = "test1.txt";
    std::ifstream ifstream1;
    //1-2) 等效地调用 rdbuf()->open(filename, mode | ios_base::in).
    ifstream1.open(strFileName1.c_str(), std::ios::in);

    std::ifstream ifstream2;
    std::string strFileName2 = "test2.txt";
    //3-4) 等效地调用 (1-2) ,如同以 open(filename.c_str(), mode) 。
    ifstream2.open(strFileName2, std::ios::in);

    std::ifstream ifstream3;
    std::string strFileName3 = "test3.txt";
    ifstream2.open(strFileName3, std::ios::in);

    std::cout << "ifstream1 is: " << (ifstream1.is_open() ? "true" : "false") << std::endl;
    std::cout << "ifstream2 is: " << (ifstream2.is_open() ? "true" : "false") << std::endl;
    std::cout << "ifstream3 is: " << (ifstream3.is_open() ? "true" : "false") << std::endl;

    std::cout << std::endl;
    return 0;
}

输出

关闭关联文件

std::basic_ifstream<CharT,Traits>::close

void close();

关闭关联文件。

等效地调用 rdbuf()->close() 。若操作期间出现错误,则调用 setstate(failbit) 。

参数

(无)

返回值

(无)

注意

此函数为 basic_ifstream 的析构函数在流对象离开作用域时调用,通常不直接调用。

调用示例

#include <fstream>
#include <utility>
#include <string>
#include <iostream>

int main()
{
    std::ifstream ifstream1("test1.txt", std::ios::in);
    std::ifstream ifstream2("test2.txt", std::ios::in);
    std::ifstream ifstream3("test3.txt", std::ios::in);
    std::cout << "ifstream1 is: " << (ifstream1.is_open() ? "true" : "false") << std::endl;
    std::cout << "ifstream2 is: " << (ifstream2.is_open() ? "true" : "false") << std::endl;
    std::cout << "ifstream3 is: " << (ifstream3.is_open() ? "true" : "false") << std::endl;

    std::cout << std::endl;
    std::cout << "std::ifstream close" << std::endl;
    ifstream1.close();
    ifstream2.close();
    ifstream3.close();

    std::cout << std::endl;

    std::cout << "ifstream1 is: " << (ifstream1.is_open() ? "true" : "false") << std::endl;
    std::cout << "ifstream2 is: " << (ifstream2.is_open() ? "true" : "false") << std::endl;
    std::cout << "ifstream3 is: " << (ifstream3.is_open() ? "true" : "false") << std::endl;

    std::cout << std::endl;
    return 0;
}

输出

非成员函数

特化 std::swap 算法

std::swap(std::basic_ifstream)

template< class CharT, class Traits >
void swap( basic_ifstream<CharT,Traits> &lhs, basic_ifstream<CharT,Traits> &rhs );

为 std::basic_ifstream 特化 std::swap 算法。交换 lhsrhs 的状态。等效地调用 lhs.swap(rhs) 。

参数

lhs, rhs-要交换状态的流

返回值

(无)

异常

(无)

调用示例

#include <fstream>
#include <utility>
#include <string>
#include <iostream>

int main()
{
    std::ifstream ifstream1("test1.txt", std::ios::in);
    std::cout << "ifstream1: " << std::endl;
    if (ifstream1.is_open())
    {
        std::string strLine;
        uint8_t index = 0;
        while (std::getline(ifstream1, strLine))
        {
            std::cout << strLine << std::endl;
            if (index > 1)
            {
                break;
            }
            index++;
        }
    }
    std::cout << std::endl;

    std::ifstream ifstream2("test2.txt", std::ios::in);
    std::cout << "ifstream2: " << std::endl;
    if (ifstream2.is_open())
    {
        std::string strLine;
        uint8_t index = 0;
        while (std::getline(ifstream2, strLine))
        {
            std::cout << strLine << std::endl;
            if (index > 1)
            {
                break;
            }
            index++;
        }
    }
    std::cout << std::endl;

    //为 std::basic_ifstream 特化 std::swap 算法。
    std::swap(ifstream1, ifstream2);

    std::cout << "ifstream1: " << std::endl;
    if (ifstream1.is_open())
    {
        std::string strLine;
        while (std::getline(ifstream1, strLine))
        {
            std::cout << strLine << std::endl;
        }
    }

    std::cout << std::endl;

    std::cout << "ifstream2: " << std::endl;
    if (ifstream2.is_open())
    {
        std::string strLine;
        while (std::getline(ifstream2, strLine))
        {
            std::cout << strLine << std::endl;
        }
    }

    return 0;
}

输出

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/825543.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

linux学习笔记(2)----汇编LED灯实验

MX6ULL 的 IO IO的复用功能 这里的只使用了低五位&#xff0c;用来配置io口&#xff0c;其中bit0~bit3(MUX_MODE)就是设置 GPIO1_IO00 的复用功能的&#xff0c;GPIO1_IO00 一共可以复用为 9种功能 IO&#xff0c;分别对应 ALT0~ALT8。每种对应了不同的功能 io的属性配置 HY…

JDBC的书写

文章目录 基本概念操作数据库方式一&#xff08;不建议使用这种查询&#xff0c;可以sql注入&#xff09;读取properties文件 事务转账示例 获取id连接池 基本概念 持久化:把数据放在磁盘上&#xff0c;断电后还是有数据。使用execute 执行增删改返回false,查返回true 操作数…

map和set的使用(基于STL库)

前言 map和set是STL模板库中重要的关联式容器&#xff0c;与序列式容器不同的是&#xff0c;关联式容器里面存储的是<key,value>结构的键值对&#xff0c;在数据检索时比序列式容器效率更高。让我们一起来看看吧&#xff01; 目录 1.set 1.1键值对 1.2set的介绍 1.3set…

点击编辑变完成

<template><div><button click"dialogshowtrue">添加部门</button><div>部门列表</div><el-table ref"multipleTable" :data"form" tooltip-effect"dark" style"width: 100%">&l…

roop 视频换脸

roop: one click face swap. 只用一张人脸图片&#xff0c;就能完成视频换脸。 项目地址&#xff1a; https://github.com/s0md3v/roopColab 部署&#xff1a; https://github.com/dream80/roop_colab 本文是本地部署的实践记录。 环境基础 OS: Ubuntu 22.04.2 LTSKernel: 5…

解决SVN或GIT忽略提交文件的问题

背景 使用IDEA 的SVN插件提交文件是总是会提交一些不需要提交的文件; 我们可以通过一些简单设置忽略这些文件。 git 在项目根目录新建文本文件&#xff0c;修改后缀为.gitignore 文件中添加内容 *.iml .project .gradle/ .idea/ target/ build/ .vscode/ .settings/ .facto…

Day51 算法记录| 动态规划 18(单调栈)

单调栈 739. 每日温度496.下一个更大元素 I503. 下一个更大元素 II42. 接雨水84. 柱状图中最大的矩形 单调栈&#xff1a;找最近的比他大的值 最近大的值&#xff1a;需要一个单调递减的栈&#xff08;大于栈顶元素就弹出&#xff09; 最近最小值&#xff1a;单调递减栈 方向&a…

【大数据】-- docker 启动 mysql 5.7,开启 binlog

1.说明 mysql binlog&#xff1a;二进制日志文件。它有两个作用&#xff0c;一是增量备份&#xff0c;即只备份新增的内容&#xff0c;可以用于恢复数据&#xff1b;二是用于主从复制等&#xff0c;即主节点维护了一个binlog日志文件&#xff0c;从节点从binlog中同步数据。 …

SAP-MM-采购收货操作错误的更正

业务场景: 工厂3000从供应商5555采购物料,下达采购订单时, 采购员错误操作收货101,实际为103, 收货后没有做105过账,后财务反馈未过账,采购员用MIGO+124将103冲销掉, 又重新用101收货,

助力工业物联网,工业大数据之客户回访事实指标需求分析【二十三】

文章目录 1&#xff1a;客户回访事实指标需求分析2&#xff1a;客户回访事实指标 1&#xff1a;客户回访事实指标需求分析 目标&#xff1a;掌握DWB层客户回访事实指标表的需求分析 路径 step1&#xff1a;目标需求step2&#xff1a;数据来源 实施 目标需求&#xff1a;基于客…

Redis实战(4)——Redisson分布式锁

1 基于互斥命令实现分布式锁的弊端 根据上篇文章基于redis互斥命令实现的分布式锁任然存在一定的弊端 1无法重入: 同一个线程无法重新获得同一把锁2超时删除 &#xff1a;会因为超时、任务阻塞而自动释放锁&#xff0c;出现其他线程抢占锁出现并行导致线程不安全的问题3 不可…

HTML基础介绍1

HTML是什么 1.HTML&#xff08;HyperText Mark-up Language&#xff09;即超文本标签语言&#xff08;可以展示的内容类型很多&#xff09; 2.HTML文本是由HTML标签组成的文本&#xff0c;可以包括文字、图形、动画、声音、表格、连接等 3.HTML的结构包括头部&#xff08;He…

AI绘画:当艺术遇见智能

&#x1f482; 个人网站:【工具大全】【游戏大全】【神级源码资源网】&#x1f91f; 前端学习课程&#xff1a;&#x1f449;【28个案例趣学前端】【400个JS面试题】&#x1f485; 寻找学习交流、摸鱼划水的小伙伴&#xff0c;请点击【摸鱼学习交流群】 前言 随着人工智能技术…

API接口给开发程序提供帮助,API接口应用价值

API可以用于开发使用相同数据的其他应用程序&#xff0c;比如公司&#xff0c;他们可以创建一个API &#xff0c;允许其他开发人员使用他们的数据并用其做其他事情&#xff0c;可以是 业务相关的 网站也可以是移动应用程序。 公司作为 信息的所有者&#xff0c; 便可以免费或收…

一次有趣的Webshell分析经历

一次有趣的Webshell分析经历 1.拉取源代码2.解密后门代码3.分析webshell逻辑4.分析404的原因5.附&#xff1a;格式化后的php代码 1.拉取源代码 在对某目标做敏感目录收集时发现对方网站备份源代码在根目录下的 backup.tar.gz&#xff0c;遂下载&#xff0c;先使用D盾分析有没有…

JS逆向-小红薯X-S环境分析

目录 前言一、分析二、验证借鉴 前言 听说这是个抓的比较严格的网址&#xff0c;这里就不作太深的分析&#xff0c;仅是将一些环境点给分析出来。 详细的可以看这位大佬的&#xff08;玩的就是一个风险转移&#xff09; 小红书x-s新版分析(2023-05-30失效) 一、分析 看波封面…

Python高阶技巧 网络编程

Socket ocket (简称 套接字) 是进程之间通信一个工具&#xff0c;好比现实生活中的插座&#xff0c;所有的家用电器要想工作都是基于插座进行&#xff0c;进程之间想要进行网络通信需要socket。 Socket负责进程之间的网络数据传输&#xff0c;好比数据的搬运工。 客户端和服务…

Element快速入门

文章目录 Element简介快速入门常见组件表格组件Pagination分页Dialog对话框Form表单 案例基本页面布局页面组件实现axios异步加载数据 vue路由打包部署 本人主攻后端&#xff0c;前端的文章基本就用来记一下的 写的文章基本没什么内容&#xff0c;还望看的多包含 Element 简介…

如何在保健品行业运用IPD?

保健品是指能调节机体功能&#xff0c;不以治疗为目的&#xff0c;并且对人体不产生任何急性、亚急性或者慢性危害的产品。保健品是食品的一个种类&#xff0c;具有一般食品的共性&#xff0c;其含有一定量的功效成分&#xff0c;能调节人体的机能&#xff0c;具有特定的功效&a…

使用Goland导出UML类图

1.安装依赖&#xff1a;goplantuml go get github.com/jfeliu007/goplantuml/parser go install github.com/jfeliu007/goplantuml/cmd/goplantumllatest 验证是否安装成功&#xff1a; 在$GOPATH的bin目录下生成.exe可执行文件&#xff1a; 2.在Goland的External Tools中添…