![]() ![]() |
|
二级C++输入输出流:屏幕输出 | |
作者:佚名 文章来源:不详 点击数 更新时间:2008/4/18 14:40:15 文章录入:杜斌 责任编辑:杜斌 | |
|
|
1、使用预定义的插入符: 最一般的屏幕输出是将插入符作用在流类对象 cout 上。 例 1 : #include <iostream.h> void main() { char *str=”string”; cout<<”the string is:”<<Str<<endl; cout<<”the address is:”<<(void*) str<<endl; } 执行结果: The string is:string the address is:0x00416d50 注意:⒈ cout<<str: 输出的是字符指针所指向的字符串。 ⒉ cout<<(void*(s)) 或 cout<<(void*)s; 功能是输出字符指针的地址值。 2、使用成员函数 put() 输出一个字符: 格式: cout.put(char c) cout.put(const char c) 功能:提供一种将字符送进输出流的方法。 例如: char c='m'; cout.put( c); // 显示字符 m. 例 2 :分析下列程序的输出结果: #include <iostream.h> void main() { cout<<'a'<<','<<'b'<<'\n'; cout.put(‘a').put(‘,').put(‘b').put(‘\n'); char c1='A',c2='B'; cout.put(c1).put(c2).put(‘\n'); } 结果: a,b a,b AB 从该程序中可以看出,使用插入符( << )可以输出字符,使用 put() 函数也可以输出字符,但是集体使用格式不同。 Put() 函数的返回值是 ostream 类的对象的引用。 3 、使用成员函数 write() 输出一个字符串。 • 格式: cout.write(const char *str,int n) • 功能:将字符串送到输出流。 例 3 : #include<iostream.h> #include<string.h> void PirntString(char *s) { cout.write(s,strlen(s)).put(‘\n'); cout.write(s,6)<<”\n”; } void main() { char str[]=”I love C++”; cout<<”the string is:”<<str<<endl; PrintString(str); PrintString(“this is a string”); } 结果: the string is:I love C++ I love C++ I love This is a string This I 说明:该程序中使用了 write() 函数输出显示字符。可以看出,它可以显示整个字符串的内容,也可以显示部分字符串的内容。 |
|
![]() ![]() |