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流介绍
目录 一:IO流的继承关系: 二:输入输出功能 1. 基本用法 2. 格式化输入 3.非格式化输入 4. 格式化输出 三:流 1. 字符流 2. 向字符流中写入数据 3. 从字符流中读出数据 4. 清空字符流 5.完整的例子 四:文件…...
华为浏览器,Chrome的平替,插件无缝连接
文章目录 背景插件书签 背景 不知道各位小伙伴有没有这样的痛点,办公电脑、家里的电脑还有手机、平板等,收藏了一个网址或者在手机上浏览了某个网页,保存起来,可是一换平台或者换个电脑,在想要浏览之前收藏的东西&…...
SpringBoot新手快速入门系列教程:前述
我自己是一个SpringBoot新手,花了一天时间学了SpringBoot。大家不要惊讶,前提是我自己已经有了10几年的编程经验精通多门语言,并且在人间最强兵器Chat某T的AI助手帮助下,才能创造一天快速学会一个框架的神话。 当然中间遇到了很多…...
C语言9 指针
目录 指针的声明与初始化 指针运算 指针的加法和减法 指针的比较 指针与数组 通过指针访问数组元素 指针与多维数组 声明指向多维数组的指针 访问多维数组元素 指针数组和数组指针 指针数组 数组指针 字符指针 字符串的定义和字符指针 直接使用字符指针初始化字…...
Floyd判圈算法——寻找重复数(C++)
287. 寻找重复数 - 力扣(LeetCode) 题目描述 给定一个包含 n 1 个整数的数组 nums ,其数字都在 [1, n] 范围内(包括 1 和 n),可知至少存在一个重复的整数。假设 nums 只有 一个重复的整数 ,返…...
面试题目分享
学习目标: 从面试了解自己的不足。 学习内容: 1.你会什么语言? 我该如何回答,我会java,c,c等,在工作中我会用到合适的语言。 牛逼吹的大话 尊敬的面试官,我精通Java和Python&…...
Solana开发之Anchor框架
文章目录 Solana开发之Anchor框架一、什么是Anchor二、安装和使用1. 安装rust2. 安装Solana下载预构建的二进制文件 3. 使用 Anchor 版本管理器 (avm) 进行安装(推荐) 四、Anchor 核心原理Anchor 程序由三部分组成程序的 ID 从哪里…...
界面组件Kendo UI for React 2024 Q2亮点 - 生成式AI集成、设计系统增强
随着最新的2024年第二季度发布,Kendo UI for React为应用程序开发设定了标准,包括生成式AI集成、增强的设计系统功能和可访问的数据可视化。新的2024年第二季度版本为应用程序界面提供了人工智能(AI)提示,从设计到代码的生产力增强、可访问性…...
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(Hadoop Distributed File System,Hadoop分布式文件系统)是Hadoop项目中的一个核心组件,旨在以高容错、高吞吐量来处理大规模数据集。它的体系架构由以下几个主要部分组成:Client,NameNo…...
大模型之战进入新赛季,开始卷应用
最近一段时间,国产大模型Kimi彻底火了,而这波爆火,某种意义上也展示了一个问题,即大模型的落地场景可能比技术比拼,更重要。 国产大模型Kimi突然爆火,与Kimi相关的产业链甚至被冠上“Kimi概念股”之名&…...
MySQL8.4.0 LTS安装教程 【小白轻松上手2024年最新长期支持版本MySQL手把手保姆级Windows超详细图文安装教程】
MySQL8.4.0 LTS安装教程 【小白轻松上手2024年最新长期支持版本MySQL手把手保姆级Windows超详细图文安装教程】 MySQL8.4.0前言(版本说明)官网下载MySQL1.访问MySQL官网2. 打开MySQL官网下载页面3. 选择下载类型Select Version【MySQL版本号】Select Ope…...
Linux 例题及详解
1.(yum)以下描述正确的是 A.在Centos中可以使用yum install 命令安装软件包 B.在Centos中可以使用yum uninstall 命令卸载软件包 C.在Centos中可以使用yum list 查看所有可安装软件包 D.在Centos中可以使用yum show查看所有可安装软件包 选项A、C是正确…...
爆款文案管理系统设计
设计一个爆款文案管理系统,目标是帮助营销团队高效地创建、管理并分析吸引人的文案,以提升产品或服务的市场吸引力和销售转化率。以下是一些关键功能和设计考量点: 1. 用户友好界面 简洁直观的界面:确保系统界面清晰,…...
FPGA-Verilog-Vivado-软件使用
这里写目录标题 1 软件配置2 FPGA-7000使用2.1 运行启动方式 1 软件配置 编辑器绑定为Vscode,粘贴VS code运行文件的目录,后缀参数保持不变: 如: D:/Users/xdwu/AppData/Local/Programs/Microsoft VS Code/Code.exe [file name]…...
Ambari Hive 创建函数无权限
作者:櫰木 1、创建udf函数 参考文档:https://blog.csdn.net/helloxiaozhe/article/details/102498567 如果已经编写好,请使用自己的。如果没有请参考以上链接进行udf函数编写。 2、创建函数遇到的问题 由于集群开启了kerberos࿰…...
ARM GEC6818 LCD绘图 实心圆 三角形 五角星 任意区域矩形以及旗帜
要在ARM上实现LCD绘图,可以按照以下步骤进行: 硬件初始化:初始化LCD控制器和相关引脚,配置时钟、分辨率和颜色深度等。 内存映射:将LCD显示区域映射到ARM的内存地址空间中,可以通过ARM的内存映射机制来实现…...
Sentinel-1 Level 1数据处理的详细算法定义(三)
《Sentinel-1 Level 1数据处理的详细算法定义》文档定义和描述了Sentinel-1实现的Level 1处理算法和方程,以便生成Level 1产品。这些算法适用于Sentinel-1的Stripmap、Interferometric Wide-swath (IW)、Extra-wide-swath (EW)和Wave模式。 今天介绍的内容如下&…...
一款永久免费的内网穿透工具——巴比达
近期,一款名为巴比达的内网穿透工具凭借其永久免费的特性,以及卓越的性能与安全性,引起了我的关注。本文将深入探讨巴比达如何通过其独创的技术方案,达到企业级数据通信要求。 WanGooe Tunnel协议 首先,巴比达的核心竞…...
翻译|解开LLMs的神秘面纱:他们怎么能做没有受过训练的事情?
大语言模型(LLMs)通过将深度学习技术与强大的计算资源结合起来,正在彻底改变我们与软件互动的方式。 虽然这项技术令人兴奋,但许多人也担忧LLMs可能生成虚假的、过时的或有问题的信息,他们有时甚至会产生令人信服的幻…...
golang循环变量捕获问题
在 Go 语言中,当在循环中启动协程(goroutine)时,如果在协程闭包中直接引用循环变量,可能会遇到一个常见的陷阱 - 循环变量捕获问题。让我详细解释一下: 问题背景 看这个代码片段: fo…...
【WiFi帧结构】
文章目录 帧结构MAC头部管理帧 帧结构 Wi-Fi的帧分为三部分组成:MAC头部frame bodyFCS,其中MAC是固定格式的,frame body是可变长度。 MAC头部有frame control,duration,address1,address2,addre…...
AI Agent与Agentic AI:原理、应用、挑战与未来展望
文章目录 一、引言二、AI Agent与Agentic AI的兴起2.1 技术契机与生态成熟2.2 Agent的定义与特征2.3 Agent的发展历程 三、AI Agent的核心技术栈解密3.1 感知模块代码示例:使用Python和OpenCV进行图像识别 3.2 认知与决策模块代码示例:使用OpenAI GPT-3进…...
【网络安全产品大调研系列】2. 体验漏洞扫描
前言 2023 年漏洞扫描服务市场规模预计为 3.06(十亿美元)。漏洞扫描服务市场行业预计将从 2024 年的 3.48(十亿美元)增长到 2032 年的 9.54(十亿美元)。预测期内漏洞扫描服务市场 CAGR(增长率&…...
第25节 Node.js 断言测试
Node.js的assert模块主要用于编写程序的单元测试时使用,通过断言可以提早发现和排查出错误。 稳定性: 5 - 锁定 这个模块可用于应用的单元测试,通过 require(assert) 可以使用这个模块。 assert.fail(actual, expected, message, operator) 使用参数…...
OpenLayers 分屏对比(地图联动)
注:当前使用的是 ol 5.3.0 版本,天地图使用的key请到天地图官网申请,并替换为自己的key 地图分屏对比在WebGIS开发中是很常见的功能,和卷帘图层不一样的是,分屏对比是在各个地图中添加相同或者不同的图层进行对比查看。…...
MySQL账号权限管理指南:安全创建账户与精细授权技巧
在MySQL数据库管理中,合理创建用户账号并分配精确权限是保障数据安全的核心环节。直接使用root账号进行所有操作不仅危险且难以审计操作行为。今天我们来全面解析MySQL账号创建与权限分配的专业方法。 一、为何需要创建独立账号? 最小权限原则…...
Reasoning over Uncertain Text by Generative Large Language Models
https://ojs.aaai.org/index.php/AAAI/article/view/34674/36829https://ojs.aaai.org/index.php/AAAI/article/view/34674/36829 1. 概述 文本中的不确定性在许多语境中传达,从日常对话到特定领域的文档(例如医学文档)(Heritage 2013;Landmark、Gulbrandsen 和 Svenevei…...
音视频——I2S 协议详解
I2S 协议详解 I2S (Inter-IC Sound) 协议是一种串行总线协议,专门用于在数字音频设备之间传输数字音频数据。它由飞利浦(Philips)公司开发,以其简单、高效和广泛的兼容性而闻名。 1. 信号线 I2S 协议通常使用三根或四根信号线&a…...
【Linux】Linux 系统默认的目录及作用说明
博主介绍:✌全网粉丝23W,CSDN博客专家、Java领域优质创作者,掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域✌ 技术范围:SpringBoot、SpringCloud、Vue、SSM、HTML、Nodejs、Python、MySQL、PostgreSQL、大数据、物…...
