当前位置: 首页 > news >正文

C++ STL IO流介绍

目录

一:IO流的继承关系:

二:输入输出功能

1. 基本用法 

 2. 格式化输入

3.非格式化输入

4. 格式化输出

三:流

1. 字符流

2. 向字符流中写入数据

3. 从字符流中读出数据

4. 清空字符流

5.完整的例子

四:文件流


一:IO流的继承关系:

含义
basic_streambuf
 
读取或写入数据
ios_base独立于字符类型的流属性
basic_ios依赖于字符类型的流属性
basic_istream用于读取数据的流基类
basic_iostream用于写入数据的流基类
basic_iostream用于读写数据的流基类

二:输入输出功能

typedef basic_istream<char> istream;
typedef basic_ostream<char> ostream;
1. 基本用法 

#include <iostream>
int main(){
std::cout << "Type in your numbers";
std::cout << "(Quit with an arbitrary character): " << std::endl;
// 2000 <Enter> 11 <a>
int sum{0};
int val;
while (std::cin >> val) sum += val;
std::cout << "Sum: " << sum; // Sum: 2011
}
 2. 格式化输入
include <iostream>int main()
{int a, b;std::cout << "Two natural numbers: " << std::endl;std::cin >> a >> b; // < 2000 11>std::cout << "a: " << a << " b: " << b;
}
3.非格式化输入
#include <iostream>int main()
{std::string line;std::cout << "Write a line: " << std::endl;std::getline(std::cin, line); // <Only for testing purpose.>std::cout << line << std::endl; // Only for testing purpose.std::cout << "Write numbers, separated by;" << std::endl;while (std::getline(std::cin, line, ';') ) {std::cout << line << " ";} 
}
4. 格式化输出
#include <iostream>int main()
{int num{2011};std::cout.setf(std::ios::hex, std::ios::basefield);std::cout << num << std::endl; // 7dbstd::cout.setf(std::ios::dec, std::ios::basefield);std::cout << num << std::endl; // 2011std::cout << std::hex << num << std::endl; // 7dbstd::cout << std::dec << num << std::endl; // 2011
}
#include <iostream>
#include <fstream>
#include <iomanip>
#include <iostream>int main()
{std::cout.fill('#');std::cout << -12345;std::cout << std::setw(10) << -12345; // ####-12345std::cout << std::setw(10) << std::left << -12345; // -12345####std::cout << std::setw(10) << std::right << -12345; // ####-12345std::cout << std::setw(10) << std::internal << -12345; //-####12345std::cout << std::oct << 2011; // 3733std::cout << std::hex << 2011; // 7dbstd::cout << std::showbase;std::cout << std::dec << 2011; // 2011std::cout << std::oct << 2011; // 03733std::cout << std::hex << 2011; // 0x7dbstd::cout << 123.456789; // 123.457std::cout << std::fixed;std::cout << std::setprecision(3) << 123.456789; // 123.457std::cout << std::setprecision(6) << 123.456789; // 123.456789std::cout << std::setprecision(9) << 123.456789; // 123.456789000std::cout << std::scientific;std::cout << std::setprecision(3) << 123.456789; // 1.235e+02std::cout << std::setprecision(6) << 123.456789; // 1.234568e+02std::cout << std::setprecision(9) << 123.456789; // 1.234567890e+02std::cout << std::hexfloat;std::cout << std::setprecision(3) << 123.456789; // 0x1.edd3c07ee0b0bp+6std::cout << std::setprecision(6) << 123.456789; // 0x1.edd3c07ee0b0bp+6std::cout << std::setprecision(9) << 123.456789; // 0x1.edd3c07ee0b0bp+6std::cout << std::defaultfloat;std::cout << std::setprecision(3) << 123.456789; // 123std::cout << std::setprecision(6) << 123.456789; // 123.457std::cout << std::setprecision(9) << 123.456789; // 123.456789}

三:流

1. 字符流
//String stream for the input of data of type char and wchar_t.
std::istringstream and std::wistringstream//String stream for the output of data of type char and wchar_t.
std::ostringstream and std::wostringstream//String stream for the input or output of data of type char and wchar_t.
std::stringstream and std::wstringstream
2. 向字符流中写入数据
std::stringstream os;
os << "New String";
os.str("Another new String");
3. 从字符流中读出数据
std::string os;
std::string str;
os >> str;
str= os.str();
4. 清空字符流
std::stringstream os;
os.str("");
5.完整的例子
#include <iostream>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>template <typename T>
T StringTo(const std::string& source) {std::istringstream iss(source);T ret;iss >> ret;return ret;
}template <typename T>
std::string ToString(const T& n) {std::ostringstream tmp;tmp << n;return tmp.str();
}int main()
{std::cout << "5= " << StringTo<int>("5"); // 5std::cout << "5 + 6= " << StringTo<int>("5") + 6; // 11std::cout << ToString(StringTo<int>("5") + 6); // "11"std::cout << "5e10: " << std::fixed << StringTo<double>("5e10"); // 50000000000
}

四:文件流

//File stream for the input of data of type char and wchar_t.
std::ifstream and std::wifstream//File stream for the output of data of type char and wchar_t.
std::ofstream and std::wofstream//File stream for the input and output of data of type char and wchar_t.
std::fstream and std::wfstream//Data buffer of type char and wchar_t.
std::filebuf and std::wfilebuf
#include <fstream>int main()
{std::ifstream in("inFile.txt");std::ofstream out("outFile.txt");out << in.rdbuf();
}
#include <fstream>
#include <iostream>
#include <istream>
#include <string>void writeFile(const std::string name) {std::ofstream outFile(name);if (!outFile) {std::cerr << "Could not open file " << name << std::endl;exit(1);}for (unsigned int i = 0; i < 10; ++i) {outFile << i << " 0123456789" << std::endl;}
}int main()
{std::string random{ "random.txt" };writeFile(random);std::ifstream inFile(random);if (!inFile) {std::cerr << "Could not open file " << random << std::endl;exit(1);}std::string line;std::cout << inFile.rdbuf();// 0 0123456789// 1 0123456789// 9 0123456789std::cout << inFile.tellg() << std::endl; // 200inFile.seekg(0); // inFile.seekg(0, std::ios::beg);std::getline(inFile, line);std::cout << line; // 0 0123456789inFile.seekg(20, std::ios::cur);std::getline(inFile, line);std::cout << line; // 2 0123456789inFile.seekg(-20, std::ios::end);std::getline(inFile, line);std::cout << line; // 9 0123456789
}

五:IO流运算符重载,支持用户自定义类型输入输出

friend std::istream& operator>> (std::istream& in, Fraction& frac);
friend std::ostream& operator<< (std::ostream& out, const Fraction& frac);
#include <fstream>
#include <iostream>
#include <istream>
#include <string>class Fraction {
public:Fraction(int num = 0, int denom = 0) :numerator(num), denominator(denom) {}friend std::istream& operator>> (std::istream& in, Fraction& frac);friend std::ostream& operator<< (std::ostream& out, const Fraction& frac);
private:int numerator;int denominator;
};
std::istream& operator>> (std::istream& in, Fraction& frac) {in >> frac.numerator;in >> frac.denominator;return in;
}
std::ostream& operator<< (std::ostream& out, const Fraction& frac) {out << frac.numerator << "/" << frac.denominator;return out;
}int main()
{Fraction frac(3, 4);std::cout << frac; // 3/4std::cout << "Enter two numbers: ";Fraction fracDef;std::cin >> fracDef; // <1 2>std::cout << fracDef; // 1/2}

相关文章:

C++ STL IO流介绍

目录 一&#xff1a;IO流的继承关系&#xff1a; 二&#xff1a;输入输出功能 1. 基本用法 2. 格式化输入 3.非格式化输入 4. 格式化输出 三&#xff1a;流 1. 字符流 2. 向字符流中写入数据 3. 从字符流中读出数据 4. 清空字符流 5.完整的例子 四&#xff1a;文件…...

华为浏览器,Chrome的平替,插件无缝连接

文章目录 背景插件书签 背景 不知道各位小伙伴有没有这样的痛点&#xff0c;办公电脑、家里的电脑还有手机、平板等&#xff0c;收藏了一个网址或者在手机上浏览了某个网页&#xff0c;保存起来&#xff0c;可是一换平台或者换个电脑&#xff0c;在想要浏览之前收藏的东西&…...

SpringBoot新手快速入门系列教程:前述

我自己是一个SpringBoot新手&#xff0c;花了一天时间学了SpringBoot。大家不要惊讶&#xff0c;前提是我自己已经有了10几年的编程经验精通多门语言&#xff0c;并且在人间最强兵器Chat某T的AI助手帮助下&#xff0c;才能创造一天快速学会一个框架的神话。 当然中间遇到了很多…...

C语言9 指针

目录 指针的声明与初始化 指针运算 指针的加法和减法 指针的比较 指针与数组 通过指针访问数组元素 指针与多维数组 声明指向多维数组的指针 访问多维数组元素 指针数组和数组指针 指针数组 数组指针 字符指针 字符串的定义和字符指针 直接使用字符指针初始化字…...

Floyd判圈算法——寻找重复数(C++)

287. 寻找重复数 - 力扣&#xff08;LeetCode&#xff09; 题目描述 给定一个包含 n 1 个整数的数组 nums &#xff0c;其数字都在 [1, n] 范围内&#xff08;包括 1 和 n&#xff09;&#xff0c;可知至少存在一个重复的整数。假设 nums 只有 一个重复的整数 &#xff0c;返…...

面试题目分享

学习目标&#xff1a; 从面试了解自己的不足。 学习内容&#xff1a; 1.你会什么语言&#xff1f; 我该如何回答&#xff0c;我会java&#xff0c;c&#xff0c;c等&#xff0c;在工作中我会用到合适的语言。 牛逼吹的大话 尊敬的面试官&#xff0c;我精通Java和Python&…...

Solana开发之Anchor框架

文章目录 Solana开发之Anchor框架一、什么是Anchor二、安装和使用1. 安装rust2. 安装Solana下载预构建的二进制文件 3. 使用 Anchor 版本管理器 &#xff08;avm&#xff09; 进行安装&#xff08;推荐&#xff09; 四、Anchor 核心原理Anchor 程序由三部分组成程序的 ID 从哪里…...

界面组件Kendo UI for React 2024 Q2亮点 - 生成式AI集成、设计系统增强

随着最新的2024年第二季度发布&#xff0c;Kendo UI for React为应用程序开发设定了标准&#xff0c;包括生成式AI集成、增强的设计系统功能和可访问的数据可视化。新的2024年第二季度版本为应用程序界面提供了人工智能(AI)提示&#xff0c;从设计到代码的生产力增强、可访问性…...

python输出/sys/class/power_supply/BAT0/电池各项内容

读取 /sys/class/power_supply/BAT0/ 目录下的所有相关文件,并输出其内容: import os# 定义电池信息文件的路径 battery_path = "/sys/class/power_supply/BAT0/"# 读取文件内容的函数 def read_battery_info(file_name):try:with open(os.path.join(battery_path…...

HDFS体系架构文件写入/下载流程

HDFS体系架构 HDFS&#xff08;Hadoop Distributed File System&#xff0c;Hadoop分布式文件系统&#xff09;是Hadoop项目中的一个核心组件&#xff0c;旨在以高容错、高吞吐量来处理大规模数据集。它的体系架构由以下几个主要部分组成&#xff1a;Client&#xff0c;NameNo…...

大模型之战进入新赛季,开始卷应用

最近一段时间&#xff0c;国产大模型Kimi彻底火了&#xff0c;而这波爆火&#xff0c;某种意义上也展示了一个问题&#xff0c;即大模型的落地场景可能比技术比拼&#xff0c;更重要。 国产大模型Kimi突然爆火&#xff0c;与Kimi相关的产业链甚至被冠上“Kimi概念股”之名&…...

MySQL8.4.0 LTS安装教程 【小白轻松上手2024年最新长期支持版本MySQL手把手保姆级Windows超详细图文安装教程】

MySQL8.4.0 LTS安装教程 【小白轻松上手2024年最新长期支持版本MySQL手把手保姆级Windows超详细图文安装教程】 MySQL8.4.0前言&#xff08;版本说明&#xff09;官网下载MySQL1.访问MySQL官网2. 打开MySQL官网下载页面3. 选择下载类型Select Version【MySQL版本号】Select Ope…...

Linux 例题及详解

1.&#xff08;yum&#xff09;以下描述正确的是 A.在Centos中可以使用yum install 命令安装软件包 B.在Centos中可以使用yum uninstall 命令卸载软件包 C.在Centos中可以使用yum list 查看所有可安装软件包 D.在Centos中可以使用yum show查看所有可安装软件包 选项A、C是正确…...

爆款文案管理系统设计

设计一个爆款文案管理系统&#xff0c;目标是帮助营销团队高效地创建、管理并分析吸引人的文案&#xff0c;以提升产品或服务的市场吸引力和销售转化率。以下是一些关键功能和设计考量点&#xff1a; 1. 用户友好界面 简洁直观的界面&#xff1a;确保系统界面清晰&#xff0c…...

FPGA-Verilog-Vivado-软件使用

这里写目录标题 1 软件配置2 FPGA-7000使用2.1 运行启动方式 1 软件配置 编辑器绑定为Vscode&#xff0c;粘贴VS code运行文件的目录&#xff0c;后缀参数保持不变&#xff1a; 如&#xff1a; D:/Users/xdwu/AppData/Local/Programs/Microsoft VS Code/Code.exe [file name]…...

Ambari Hive 创建函数无权限

作者&#xff1a;櫰木 1、创建udf函数 参考文档&#xff1a;https://blog.csdn.net/helloxiaozhe/article/details/102498567 如果已经编写好&#xff0c;请使用自己的。如果没有请参考以上链接进行udf函数编写。 2、创建函数遇到的问题 由于集群开启了kerberos&#xff0…...

ARM GEC6818 LCD绘图 实心圆 三角形 五角星 任意区域矩形以及旗帜

要在ARM上实现LCD绘图&#xff0c;可以按照以下步骤进行&#xff1a; 硬件初始化&#xff1a;初始化LCD控制器和相关引脚&#xff0c;配置时钟、分辨率和颜色深度等。 内存映射&#xff1a;将LCD显示区域映射到ARM的内存地址空间中&#xff0c;可以通过ARM的内存映射机制来实现…...

Sentinel-1 Level 1数据处理的详细算法定义(三)

《Sentinel-1 Level 1数据处理的详细算法定义》文档定义和描述了Sentinel-1实现的Level 1处理算法和方程&#xff0c;以便生成Level 1产品。这些算法适用于Sentinel-1的Stripmap、Interferometric Wide-swath (IW)、Extra-wide-swath (EW)和Wave模式。 今天介绍的内容如下&…...

一款永久免费的内网穿透工具——巴比达

近期&#xff0c;一款名为巴比达的内网穿透工具凭借其永久免费的特性&#xff0c;以及卓越的性能与安全性&#xff0c;引起了我的关注。本文将深入探讨巴比达如何通过其独创的技术方案&#xff0c;达到企业级数据通信要求。 WanGooe Tunnel协议 首先&#xff0c;巴比达的核心竞…...

翻译|解开LLMs的神秘面纱:他们怎么能做没有受过训练的事情?

大语言模型&#xff08;LLMs&#xff09;通过将深度学习技术与强大的计算资源结合起来&#xff0c;正在彻底改变我们与软件互动的方式。 虽然这项技术令人兴奋&#xff0c;但许多人也担忧LLMs可能生成虚假的、过时的或有问题的信息&#xff0c;他们有时甚至会产生令人信服的幻…...

【OSG学习笔记】Day 18: 碰撞检测与物理交互

物理引擎&#xff08;Physics Engine&#xff09; 物理引擎 是一种通过计算机模拟物理规律&#xff08;如力学、碰撞、重力、流体动力学等&#xff09;的软件工具或库。 它的核心目标是在虚拟环境中逼真地模拟物体的运动和交互&#xff0c;广泛应用于 游戏开发、动画制作、虚…...

线程同步:确保多线程程序的安全与高效!

全文目录&#xff1a; 开篇语前序前言第一部分&#xff1a;线程同步的概念与问题1.1 线程同步的概念1.2 线程同步的问题1.3 线程同步的解决方案 第二部分&#xff1a;synchronized关键字的使用2.1 使用 synchronized修饰方法2.2 使用 synchronized修饰代码块 第三部分&#xff…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院挂号小程序

一、开发准备 ​​环境搭建​​&#xff1a; 安装DevEco Studio 3.0或更高版本配置HarmonyOS SDK申请开发者账号 ​​项目创建​​&#xff1a; File > New > Create Project > Application (选择"Empty Ability") 二、核心功能实现 1. 医院科室展示 /…...

python爬虫:Newspaper3k 的详细使用(好用的新闻网站文章抓取和解析的Python库)

更多内容请见: 爬虫和逆向教程-专栏介绍和目录 文章目录 一、Newspaper3k 概述1.1 Newspaper3k 介绍1.2 主要功能1.3 典型应用场景1.4 安装二、基本用法2.2 提取单篇文章的内容2.2 处理多篇文档三、高级选项3.1 自定义配置3.2 分析文章情感四、实战案例4.1 构建新闻摘要聚合器…...

【配置 YOLOX 用于按目录分类的图片数据集】

现在的图标点选越来越多&#xff0c;如何一步解决&#xff0c;采用 YOLOX 目标检测模式则可以轻松解决 要在 YOLOX 中使用按目录分类的图片数据集&#xff08;每个目录代表一个类别&#xff0c;目录下是该类别的所有图片&#xff09;&#xff0c;你需要进行以下配置步骤&#x…...

css的定位(position)详解:相对定位 绝对定位 固定定位

在 CSS 中&#xff0c;元素的定位通过 position 属性控制&#xff0c;共有 5 种定位模式&#xff1a;static&#xff08;静态定位&#xff09;、relative&#xff08;相对定位&#xff09;、absolute&#xff08;绝对定位&#xff09;、fixed&#xff08;固定定位&#xff09;和…...

【Java_EE】Spring MVC

目录 Spring Web MVC ​编辑注解 RestController RequestMapping RequestParam RequestParam RequestBody PathVariable RequestPart 参数传递 注意事项 ​编辑参数重命名 RequestParam ​编辑​编辑传递集合 RequestParam 传递JSON数据 ​编辑RequestBody ​…...

06 Deep learning神经网络编程基础 激活函数 --吴恩达

深度学习激活函数详解 一、核心作用 引入非线性:使神经网络可学习复杂模式控制输出范围:如Sigmoid将输出限制在(0,1)梯度传递:影响反向传播的稳定性二、常见类型及数学表达 Sigmoid σ ( x ) = 1 1 +...

QT: `long long` 类型转换为 `QString` 2025.6.5

在 Qt 中&#xff0c;将 long long 类型转换为 QString 可以通过以下两种常用方法实现&#xff1a; 方法 1&#xff1a;使用 QString::number() 直接调用 QString 的静态方法 number()&#xff0c;将数值转换为字符串&#xff1a; long long value 1234567890123456789LL; …...

关键领域软件测试的突围之路:如何破解安全与效率的平衡难题

在数字化浪潮席卷全球的今天&#xff0c;软件系统已成为国家关键领域的核心战斗力。不同于普通商业软件&#xff0c;这些承载着国家安全使命的软件系统面临着前所未有的质量挑战——如何在确保绝对安全的前提下&#xff0c;实现高效测试与快速迭代&#xff1f;这一命题正考验着…...