C++ Ostream 库 - write
描述
它用于将 s 指向的数组的前 n 个字符插入到流中。 此函数只是复制一个数据块,而不检查其内容:该数组可能包含空字符,这些字符也可以在不停止复制过程的情况下进行复制。
声明
以下是 std::ostream::write 的声明。
ostream& write (const char* s, streamsize n);
参数
s − 指向至少包含 n 个字符的数组的指针。
n − 要插入的字符数。
返回值
它返回 ostream 对象 (*this)。
异常
Basic guarantee − 如果抛出异常,则对象处于有效状态。
数据竞争
修改流 objectAccess 最多由 s 指向的 n 个字符。/p>
示例
在下面的例子中解释了 std::ostream::write。
#include <fstream>
int main () {
std::ifstream infile ("test.txt",std::ifstream::binary);
std::ofstream outfile ("new.txt",std::ofstream::binary);
infile.seekg (0,infile.end);
long size = infile.tellg();
infile.seekg (0);
char* buffer = new char[size];
infile.read (buffer,size);
outfile.write (buffer,size);
delete[] buffer;
outfile.close();
infile.close();
return 0;
}

