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可能生成虚假的、过时的或有问题的信息,他们有时甚至会产生令人信服的幻…...
synchronized 学习
学习源: https://www.bilibili.com/video/BV1aJ411V763?spm_id_from333.788.videopod.episodes&vd_source32e1c41a9370911ab06d12fbc36c4ebc 1.应用场景 不超卖,也要考虑性能问题(场景) 2.常见面试问题: sync出…...
树莓派超全系列教程文档--(61)树莓派摄像头高级使用方法
树莓派摄像头高级使用方法 配置通过调谐文件来调整相机行为 使用多个摄像头安装 libcam 和 rpicam-apps依赖关系开发包 文章来源: http://raspberry.dns8844.cn/documentation 原文网址 配置 大多数用例自动工作,无需更改相机配置。但是,一…...
Xshell远程连接Kali(默认 | 私钥)Note版
前言:xshell远程连接,私钥连接和常规默认连接 任务一 开启ssh服务 service ssh status //查看ssh服务状态 service ssh start //开启ssh服务 update-rc.d ssh enable //开启自启动ssh服务 任务二 修改配置文件 vi /etc/ssh/ssh_config //第一…...
QMC5883L的驱动
简介 本篇文章的代码已经上传到了github上面,开源代码 作为一个电子罗盘模块,我们可以通过I2C从中获取偏航角yaw,相对于六轴陀螺仪的yaw,qmc5883l几乎不会零飘并且成本较低。 参考资料 QMC5883L磁场传感器驱动 QMC5883L磁力计…...
《Playwright:微软的自动化测试工具详解》
Playwright 简介:声明内容来自网络,将内容拼接整理出来的文档 Playwright 是微软开发的自动化测试工具,支持 Chrome、Firefox、Safari 等主流浏览器,提供多语言 API(Python、JavaScript、Java、.NET)。它的特点包括&a…...
鱼香ros docker配置镜像报错:https://registry-1.docker.io/v2/
使用鱼香ros一件安装docker时的https://registry-1.docker.io/v2/问题 一键安装指令 wget http://fishros.com/install -O fishros && . fishros出现问题:docker pull 失败 网络不同,需要使用镜像源 按照如下步骤操作 sudo vi /etc/docker/dae…...
c#开发AI模型对话
AI模型 前面已经介绍了一般AI模型本地部署,直接调用现成的模型数据。这里主要讲述讲接口集成到我们自己的程序中使用方式。 微软提供了ML.NET来开发和使用AI模型,但是目前国内可能使用不多,至少实践例子很少看见。开发训练模型就不介绍了&am…...
【JavaSE】绘图与事件入门学习笔记
-Java绘图坐标体系 坐标体系-介绍 坐标原点位于左上角,以像素为单位。 在Java坐标系中,第一个是x坐标,表示当前位置为水平方向,距离坐标原点x个像素;第二个是y坐标,表示当前位置为垂直方向,距离坐标原点y个像素。 坐标体系-像素 …...
大数据学习(132)-HIve数据分析
🍋🍋大数据学习🍋🍋 🔥系列专栏: 👑哲学语录: 用力所能及,改变世界。 💖如果觉得博主的文章还不错的话,请点赞👍收藏⭐️留言Ǵ…...
Android第十三次面试总结(四大 组件基础)
Activity生命周期和四大启动模式详解 一、Activity 生命周期 Activity 的生命周期由一系列回调方法组成,用于管理其创建、可见性、焦点和销毁过程。以下是核心方法及其调用时机: onCreate() 调用时机:Activity 首次创建时调用。…...
