19.21/0标准流类运行结果:value = 100&value=0019FF2C例19-4:指针值的显示iptr=0019FF2C#include<iostream>&iptr=0019FF28using namespace std;void main()(int value = 100 ;int * iptr = & value ;//显示value值cout <<" value = " << value << endl ;//显示value的存放地址cout <<" &value =" << &value << endl ;//显示iptr值(保存了value的地址)cout <<" iptr =" <<iptr <<endl ;//显示iptr自身地址cout <<" &iptr =" << &iptr << endl ;
例19-4:指针值的显示 #include<iostream> using namespace std ; void main( ) { int value = 100 ; int * iptr = & value ; //显示value值 cout << " value = " << value << endl ; //显示value的存放地址 cout << " &value = " << &value << endl ; //显示iptr值(保存了value的地址) cout << " iptr = " << iptr << endl ; //显示iptr自身地址 cout << " &iptr = " << &iptr << endl ; } 运行结果: value = 100 &value = 0019FF2C iptr = 0019FF2C &iptr = 0019FF28
19.21/0标准流类例19-5:使用提取运算符进行数据的连续输入#include<iostream>运行结果:using namespacestd;请输入i、f和str:void main( )12.22 TestStream{数据分别为:int i;1 2.22 TestStreamdouble f ;char str[30] ;cout<<"请输入i、f和str:"cin >>i>> f>> str ;cout<<"数据分别为:n"<<i<<","<<f<<""<<str<<endl;
例19-5:使用提取运算符进行数据的连续输入 #include<iostream> using namespace std ; void main( ) { int i ; double f ; char str[30] ; cout << " 请输入i、f和str:" ; cin >> i >> f >> str ; cout << "数据分别为:\n" << i << "," <<f<< "," <<str<<endl ; } 运行结果: 请输入i、f和str: 1 2.22 TestStream 数据分别为: 1 2.22 TestStream
文件流类19.3ofstream、ifstream和fstream是文件流类其中,fstream是ofstream和ifstream多重继承的子类。文件流类不是标准设备,所以没有cout那样预先定义的全局对象文件流类定义的操作应用于外部设备,最典型的是文件
• ofstream、ifstream和fstream是文件流类。 • 其中,fstream是ofstream和ifstream多重继承 的子类。 • 文件流类不是标准设备,所以没有cout那样 预先定义的全局对象。 • 文件流类定义的操作应用于外部设备,最典 型的是文件
文件流类19.3,要定义一个文件流类对象,须定义文件名和打开方式。类ofstream用于执行文件输出,该类有几个构造函数。要打开的文件名·其中,最常用的是:ofstream::ofstream(char * pFilename,文件打开方式int mode = ios::cout,int prot = filebuf::openprot) ;文件保护方式
• 要定义一个文件流类对象,须定义文件名和 打开方式。 • 类ofstream用于执行文件输出,该类有几个 构造函数。 • 其中,最常用的是: ofstream::ofstream(char * pFilename, int mode = ios::cout, int prot = filebuf::openprot) ; 要打开的文件名 文件打开方式 文件保护方式
文件流类19.3表19-2文件打开选项标志含义ios::ate如果文件存在,输出内容加在末尾ios::in具有输入功能具有输出功能ios::outios::trunc如果文件存在,清除文件内容如果文件不存在,返回错误ios::nocreateios::noreplace如果文件存在,返回错误ios::binary以二进制方式打开文件
表19-2 文件打开选项 标 志 含 义 ios::ate 如果文件存在,输出内容加在末尾 ios::in 具有输入功能 ios::out 具有输出功能 ios::trunc 如果文件存在,清除文件内容 ios::nocreate 如果文件不存在,返回错误 ios::noreplace 如果文件存在,返回错误 ios::binary 以二进制方式打开文件