什么目的地<<源.rdbuf();在做什么?为什么太多的开盘关闭

what destination << source.rdbuf(); is doing ? why too many open close

本文关键字:什么 lt 为什么 太多 开盘 目的地 rdbuf      更新时间:2023-10-16
if (!source.is_open() || !destination.is_open()) 
{   
    if (source.is_open())
    {source.close();
    }
    if (destination.is_open())
    {destination.close();
    }
    return -1;
}
destination << source.rdbuf();
source.close();
destination.close();
return  0;

destination大概是输出字符流。<<是一个二进制操作员,将右手操作数插入左手操作数的输出流中。在这种情况下,插入了source.rdbuf()的结果。

source.rdbuf()是一个函数调用。rdbufsource的成员函数。source大概在输入字符流中。在此假设下,rdbuf的行为是根据文档的:

返回关联的流缓冲区。

将流缓冲区插入输出流的行为是根据文档的:

从[流缓冲区]控制的输入序列中提取字符,并将其插入[输出流],直到以下条件之一

  • 文件结束发生在输入序列上;
  • ...

作为一个例子,如果destination是字符串流,而source是文件流,则在操作后,字符串流的缓冲区内的字符串将包含文件的整个内容。