C++之istream与ostream

文章目录

  • ios类是istream类和ostream类的虚基类,用来提供对流进行格式化I/O操作和错误处理的成员函数
  • streambuf主要作为其他类的支持,定义了对缓冲区的通用操作,如设置缓冲区、从缓冲区中读取数据,写入数据等操作
  • 为了便于程序数据的输入输出,C++预定义了几个标准输入输出流对象:
    • cout: ostream cout, 与标准输出设备关联
    • cin: istream cin, 与表示输入设备关联
    • cerr: ostream cerr, 与标准错误设备关联(非缓冲方式)
    • clog: ostream clog, 与标准错误设备关联(缓冲方式)

istream与ostream

cpp 复制代码
#include <iostream>
using namespace std;
int main(void){
    char a[100] = {0};
    cout << "please input a string: ";
    //遇到空格就结束
    cin >> a; // 123 456
    cout << a << endl;
    return 0;
}

istream

  • istream类定义了许多用于从流中提取数据和操作文件的成员函数,并对流的析取运算符>>进行了重载,实现了对内置数据量的输入功能
  • 其中常用的几个成员函数是get、getline、read 、ignore
cpp 复制代码
//从输入流中提取一个字符(包括空白符)
get(char_type& ch); 
//一次性读取一行字符
getline( char_type* s, std::streamsize count, char_type delim );
//从输入流一次性读取count个字符
read( char_type* s, std::streamsize count);
//从输入流中读取字符并丢弃
ignore(std::streamsize count = 1, int_type delim = Traits::eof())
cpp 复制代码
#include <iostream>
using namespace std;
int main(void){
    char a[100] = {0};
    char c ;
    cout << "please input a string: ";
    //遇到空格就结束
    //cin >> a; // 123 456
    cin.getline(a, 100);
    cout << a << endl;
    cout << "use get(), please input char: ";
    while((c = cin.get()) != '\n')
        cout << c;
    cout << endl;
    cout << "use get(a, 10) input char: ";
    cin.get(a, 10);
    cout << a <<endl;
    return 0;
}

ostream

  • ostream类提供了许多用于数据输出的成员函数,并通过流的输出<<重载,实现了对内置数据类型的输出功能。
  • 其中几个常用的成员函数是put、write、flush
cpp 复制代码
//插入一个字符到输出流
put(char_type ch ); 
//插入一个字符序列到输出流中
write(const char_type* s, std::streamsize count);
//刷新输出流
flush();
cpp 复制代码
#include <iostream>
#include <cstring>
using namespace std;
int main(void){
    char a[100] = "你好";
    cout << "hello" << endl;
    cout.put('w').put('o').put('r').put('l').put('d').put('\n');
    cout.write(a, strlen(a));
    return 0;
}

输入输出的格式控制

  • C语言中可以使用scanf()和printf()实现数据的格式化输入输出,C++中利用ios类的格式控制成员函数或者操纵符进行输入、输出数据的格式化

操纵符(全局函数)

cpp 复制代码
#include <iostream>
#include <iomanip>
using namespace std;
    int main(void){
    cout << 10/3.0 << endl;
    cout << setprecision(10) << 10/3.0 << endl;
    cout << setw(10) << 1234567890 << endl;
    cout << setw(10) << setfill('0') << 123 << endl;
    cout << setw(8) << left << setfill(' ') << hex << showbase << 100 << endl;
    return 0;
}

成员函数

cpp 复制代码
#include <iostream>
#include <iomanip>
using namespace std;
int main(void){
    cout << 10/3.0 << endl;
    // cout << setprecision(10) << 10/3.0 << endl;
    cout.precision(10);
    cout << 10/3.0 << endl;
    cout << 1234567890 << endl;
    //cout << setw(10) << setfill('0') << 123 << endl;
    cout.width(10);
    cout.fill('0');
    cout << 123 << endl;
    //cout << setw(8) << left << setfill(' ') << hex << showbase << 100 << endl;
    cout.width(8);
    cout.fill(' ');
    cout.setf(ios::hex, ios::basefield);
    cout.setf(ios::showbase);
    cout << 100 << endl;
    return 0;
}
相关推荐
信竞星球_少儿编程题库1 分钟前
2026年全国青少年信息素养大赛算法应用主题赛(星火征途C++小学组复赛真题)
c++
盐焗鹌鹑蛋1 小时前
【C++】AVL树
c++
杜子不疼.1 小时前
【C++】继承—C++的秘密武器,get父类的智慧
开发语言·c++
ShineWinsu2 小时前
对于Linux:基于UDP实现简单聊天室功能
linux·c++·面试·udp·笔试·进程·简单聊天室
chase_my_dream2 小时前
2D-SLAM 真实数据处理与多传感器工程落地:时间同步、异常过滤、标定对齐和系统调试
c++·人工智能·2d-slam
c238562 小时前
《序列 DP:C++ 中的“最长”套路与编辑距离》
c++·算法·动态规划
鱼子星_2 小时前
【C++】类和对象(下)——初始化列表,类型转换,static成员,友元,内部类,匿名对象,编译器的优化拓展
c语言·c++·笔记
王老师青少年编程10 小时前
2025年【江苏“信息与未来”编程思维】真题及题解(T2:坐标变换)
c++·题解·真题·坐标变换·编程思维·江苏·信息与未来
2023自学中14 小时前
C++ 内存追踪器
linux·c++
DogDaoDao19 小时前
【GitHub】 LLVM Project 深度解析:现代编译器基础设施的基石
java·c++·python·程序员·github·编译器·llvm