C++之ostream操作函数operator<<、operator=、put、write、tellp、seekp、flush、swap总结(二百零八)

简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!

优质专栏:Audio工程师进阶系列原创干货持续更新中......】🚀

人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.
更多原创,欢迎关注:Android系统攻城狮

1.前言

本篇目的:理解C++之ostream函数operator<<、operator=、put、write、tellp、seekp、flush、swap用法。

    1. operator<<: 重载输出运算符,用于向ostream对象写入数据。例如:cout << "Hello, World!"
    1. operator=: 重载赋值运算符,用于将一个ostream对象的状态赋值到另一个ostream对象。
    1. put: 将一个字符写入ostream对象。
    1. write: 将一定数量的字节从指定的内存地址写入ostream对象。
    1. tellp: 返回当前写指针的位置,即当前写入的位置。返回的是一个streampos类型的对象。
    1. seekp: 将写指针移动到指定的位置,以便进行随机访问写入。可以使用seekp(pos)将写指针设置为指定位置,或者使用seekp(offset, origin)根据偏移量和起始位置移动写指针。
    1. flush: 清空输出缓冲区,确保所有待写入的数据被立即写入目标设备。
    1. swap: 交换两个ostream对象的内容。

2.应用实例

1. operator<<:用于将数据插入到输出流中。它通过重载操作符<<来实现,常用于将各种数据类型输出到流中。例如:

cpp 复制代码
#include <iostream>

int main() {
  int num = 10;
  std::cout << "The number is: " << num << std::endl;
  return 0;
}

2. operator=:用于将一个流对象的属性值赋值给另一个流对象。例如:

cpp 复制代码
#include <iostream>

int main() {
  std::ostream obj1(std::cout.rdbuf());
  std::ostream obj2(std::cerr.rdbuf());

  obj2 = obj1;

  obj1 << "This will be printed on standard output." << std::endl;
  obj2 << "This will be printed on standard error." << std::endl;

  return 0;
}

3. operator!:用于检查流是否发生了错误。它返回true表示流发生了错误,返回false表示流没有发生错误。例如:

cpp 复制代码
#include <iostream>

int main() {
  std::ofstream file("example.txt");
  if (!file) {
    std::cout << "Error opening file." << std::endl;
  } else {
    file << "Writing data to the file." << std::endl;
    file.close();
  }
  return 0;
}

4. put:用于将一个字符插入到输出流中。它接受一个字符参数,并将该字符插入到流中。例如:

cpp 复制代码
#include <iostream>

int main() {
  char ch = 'A';
  std::cout.put(ch);
  return 0;
}

5. write:用于将一定数量的字符写入到输出流中。它接受一个字符数组和一个长度参数,并将该字符数组中的字符写入到流中。例如:

cpp 复制代码
#include <iostream>

int main() {
  char buffer[] = "Hello World!";
  std::cout.write(buffer, sizeof(buffer) - 1);
  return 0;
}

6. tellp:用于获取当前的写入位置。它返回一个流位置类型的值,表示当前写入位置的偏移量。例如:

cpp 复制代码
#include <iostream>

int main() {
  std::ofstream file("example.txt", std::ios::app);
  file << "This is a line of text." << std::endl;

  std::streampos pos = file.tellp();
  std::cout << "Current write position: " << pos << std::endl;

  file.close();
  return 0;
}

7. seekp:用于设置写入位置。它接受一个流位置类型的值,表示要设置的写入位置的偏移量。例如:

cpp 复制代码
#include <iostream>

int main() {
  std::ofstream file("example.txt", std::ios::app);
  
  file << "First line" << std::endl;
  std::streampos pos = file.tellp();

  file.seekp(pos);
  file << "Second line" << std::endl;

  file.close();
  return 0;
}

8. flush:用于刷新输出流。它会清空输出流的缓冲区,将缓冲区中的内容立即写入到输出设备。例如:

cpp 复制代码
#include <iostream>
#include <thread>

int main() {
  std::cout << "Flushing ";
  std::this_thread::sleep_for(std::chrono::seconds(1));
  std::cout.flush();
  std::cout << "done." << std::endl;

  return 0;
}

9. swap:用于交换两个流对象的属性值。它接受一个流对象作为参数,并将调用它的对象与参数之间的属性值进行交换。例如:

cpp 复制代码
#include <iostream>

int main() {
  std::ostream obj1(std::cout.rdbuf());
  std::ostream obj2(std::cerr.rdbuf());

  std::swap(obj1, obj2);

  obj1 << "This will be printed on standard error." << std::endl;
  obj2 << "This will be printed on standard output." << std::endl;

  return 0;
}
相关推荐
大白同学42137 分钟前
【C++】C++11介绍(Ⅱ)
开发语言·c++
你怎么知道我是队长1 小时前
C语言---存储类
c语言·开发语言
XIAOYU6720131 小时前
金融数学专业需要学哪些数学和编程内容?
开发语言·matlab·金融
油炸自行车1 小时前
【Qt】编写Qt自定义Ui控件步骤
开发语言·c++·qt·ui·自定义ui控件·qt4 自定义ui控件
浪扼飞舟1 小时前
c#基础二(类和对象,构造器调用顺序、访问级别、重写和多态、抽象类和接口)
java·开发语言·c#
yuanpan2 小时前
python标准库有哪些模块,简单总结下。
开发语言·python
听情歌落俗2 小时前
MATLAB3-2数据存储-台大郭彦甫
开发语言·数学建模·matlab·矩阵
云天徽上2 小时前
【数据可视化-112】使用PyEcharts绘制TreeMap(矩形树图)完全指南及电商销售数据TreeMap绘制实战
开发语言·python·信息可视化·数据分析·pyecharts
呱呱巨基3 小时前
C/C++ 内存管理
c++·笔记·学习
半桔3 小时前
【网络编程】TCP 服务器并发编程:多进程、线程池与守护进程实践
linux·服务器·网络·c++·tcp/ip