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;
}
相关推荐
程序员老舅3 小时前
干货|腾讯 Linux C/C++ 后端开发岗面试
linux·c语言·c++·编程·大厂面试题
程序员Aries3 小时前
自定义网络协议与序列化/反序列化
linux·网络·c++·网络协议·程序人生
Pafey4 小时前
MFC中一个类的成员变量值自动被篡改:多重继承带来的问题
c++·mfc
hsjkdhs4 小时前
C++之多层继承、多源继承、菱形继承
开发语言·c++·算法
Full Stack Developme4 小时前
Python Redis 教程
开发语言·redis·python
阳光雨滴4 小时前
使用wpf用户控件编程落石效果动画
c++·wpf
qq_4924484465 小时前
Jmeter设置负载阶梯式压测场景(详解教程)
开发语言·python·jmeter
小贾要学习5 小时前
【数据结构】C++实现红黑树
数据结构·c++
ID_180079054735 小时前
京东获取整站实时商品详情数据|商品标题|数据分析提取教程
java·开发语言
ajassi20005 小时前
开源 C++ QT QML 开发(十七)进程--LocalSocket
c++·qt·开源