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;
}
相关推荐
七七&5561 小时前
2024年08月13日 Go生态洞察:Go 1.23 发布与全面深度解读
开发语言·网络·golang
java坤坤1 小时前
GoLand 项目从 0 到 1:第八天 ——GORM 命名策略陷阱与 Go 项目启动慢问题攻坚
开发语言·后端·golang
元清加油2 小时前
【Golang】:函数和包
服务器·开发语言·网络·后端·网络协议·golang
健康平安的活着2 小时前
java之 junit4单元测试Mockito的使用
java·开发语言·单元测试
No0d1es2 小时前
电子学会青少年软件编程(C/C++)5级等级考试真题试卷(2024年6月)
c语言·c++·算法·青少年编程·电子学会·五级
DjangoJason3 小时前
C++ 仿RabbitMQ实现消息队列项目
开发语言·c++·rabbitmq
m0_480502644 小时前
Rust 入门 KV存储HashMap (十七)
java·开发语言·rust
大阳1234 小时前
线程(基本概念和相关命令)
开发语言·数据结构·经验分享·算法·线程·学习经验
YA3334 小时前
java基础(九)sql基础及索引
java·开发语言·sql
奇树谦5 小时前
QT|windwos桌面端应用程序开发,当连接多个显示器的时候,如何获取屏幕编号?
开发语言·qt