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;
}
相关推荐
励志的大鹰哥8 分钟前
V少JS基础班之第七弹
开发语言·javascript·ecmascript
AI360labs_atyun38 分钟前
Java在AI时代的演进与应用:一个务实的视角
java·开发语言·人工智能·科技·学习·ai
凤年徐1 小时前
【数据结构与算法】203.移除链表元素(LeetCode)图文详解
c语言·开发语言·数据结构·算法·leetcode·链表·刷题
nbsaas-boot2 小时前
多租户架构下的多线程处理实践指南
java·开发语言·spring
无小道2 小时前
c++--typedef和#define的用法及区别
c语言·开发语言·汇编·c++
SoniaChen332 小时前
Rust基础-part2-变量和可变类型
开发语言·后端·rust
mit6.8243 小时前
[Vroom] 位置与矩阵 | 路由集成 | 抽象,解耦与通信
c++·人工智能·算法
ChuHsiang3 小时前
【C++】模板(二)
c++
神所夸赞的夏天3 小时前
c#获取Datatable中某列最大或最小的行数据方法
开发语言·c#
h0l10w3 小时前
【Java】MongoDB
java·开发语言·mongodb