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;
}
相关推荐
wangjinjin18029 分钟前
Python Excel 文件处理:openpyxl 与 pandas 库完全指南
开发语言·python
愚润求学29 分钟前
【C++】类型转换
开发语言·c++
@我漫长的孤独流浪37 分钟前
数据结构测试模拟题(4)
数据结构·c++·算法
斯奕sky_small-BAD41 分钟前
C++ if语句完全指南:从基础到工程实践
java·开发语言·php
Humbunklung1 小时前
Rust Floem UI 框架使用简介
开发语言·ui·rust
csdnzzt1 小时前
从内存角度透视现代C++关键特性
c++
jie188945758661 小时前
C++ 中的 const 知识点详解,c++和c语言区别
java·c语言·c++
网安INF1 小时前
RSA加密算法:非对称密码学的基石
java·开发语言·密码学
明月*清风1 小时前
c++ —— 内存管理
开发语言·c++
蔡蓝2 小时前
设计模式-观察着模式
java·开发语言·设计模式