ostream&write(const char * s,streamsize n);
其中,s 用于指定某个长度至少为 n 的字符数组或字符串;n 表示要输出的前 n 个字符。cout.write(c1, 1).write(c2, 2).write(c3, 3);
因为 cout.write(c1, 1) 向输出流缓冲区中添加 c1 字符串中第 1 字符的同时,会返回一个引用形式的 cout 对象,所以可以继续用此对象调用 write(c2, 2),向输出流缓冲区添加 c2 字符串中前 2 个字符,依次类推。
#include <iostream>
using namespace std;
int main() {
const char * str = "http://c.biancheng.net/cplus/";
cout.write(str, 4);
return 0;
}
程序执行结果为:
http
#include <iostream>
using namespace std;
int main() {
cout.write("http://", 7).write("c.biancheng.net", 15).write("/cplus/", 7);
return 0;
}
程序执行结果为:
http://c.biancheng.net/cplus/
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有