📄 iostream.cpp
字号:
#include <iostream.h>
#include <string.h>
//【例16.1】 利用流cerr与clog作为输入提示与输出结果。
void main16_1()
{
int i;
cerr<<"Please input a integer number:";
cin>>i;
clog<<"The integer number is "<<i;
clog<<endl; //刷新输出缓冲区 注意与clog<<'\n'和clog<<'\r'的区别
cerr<<"End."<<'\n';
}
//【例16.2】 利用ostream类中重载的插入运算符"<<"输出系统预定义类型的数据,包括字符串和指针类型。
void main16_2()
{
int a=3;
char* string="Hello!";
cout<<string;
cout<<endl;
cout<<"a="<<a<<endl;
cout<<"4*5="<<(4*5)<<endl;
cout<<"the address of string is "<<(void*)string<<endl;
}
//【例16.3】 利用ostream类中的成员函数put()输出字符信息。
void main16_3()
{
char ch1='H',ch2='i',ch3='!';
cout.put('H');
cout.put('i');
cout.put('\n');
cout.put(ch1).put(ch2).put(ch3).put('\n');
}
//【例16.4】 利用ostream类中的成员函数write()输出字符串信息。
void main16_4()
{
char* string="Welcome to C++!";
cout.write(string,strlen(string)).write("\n",1);
cout.write(string,10).put('\n');
cout.write(string,7)<<endl;
}
//【例16.5】 利用重载插入符"<<"实现复数类数据的输出。
class Complex
{
double rpart,ipart;
public:
Complex(double r=0.0,double i=0.0)
{
rpart=r;
ipart=i;
}
friend ostream& operator<<(ostream&,Complex&);
};
ostream& operator<<(ostream& os,Complex& c)
{
os<<"("<<c.rpart<<","<<c.ipart<<")\n";
return os;
}
void main16_5()
{
Complex a(5,4),b(1.1,2.2);
cout<<a<<b;
}
//【例16.6】 利用istream类中重载的提取运算符">>"输入系统基本数据类型的数据。
void main16_6()
{
int a,b;
char* string=new char[20];
cin>>a>>b;
cin>>string;
cout<<"a="<<a<<","<<"b="<<b<<endl;
cout<<"string="<<string<<endl;
}
//【例16.7】 利用istream类中的成员函数get()或getline()输入字符信息。
void main16_7()
{
const int SIZE=80;
char ch1,ch2,ch3;
char buffer1[SIZE],buffer2[SIZE];
ch1=cin.get(); //采用不带参数的get()函数形式输入一个字符
cin.get(ch2).get(ch3); //采用带一个参数get()函数形式连续输入一个、一个字符
cin.get(buffer1,5); //采用带三个参数get()函数形式输入一串字符
cout<<ch1<<ch2<<ch3<<endl;
cout<<"buffer1="<<buffer1<<endl;
cin.getline(buffer2,SIZE); //采用带三个参数getline()函数形式输入一行字符
cout<<"buffer2="<<buffer2<<endl;
}
//【例16.8】 利用istream类中的成员函数read()输入字符信息。
void main16_8()
{
const int SIZE=80;
char buffer1[SIZE]="",buffer2[SIZE]="";
cin.read(buffer1,5).read(buffer2,5);
cout<<endl;
cout<<buffer1<<endl;
cout<<buffer2<<endl;
}
//【例16.9】 利用重载提取符">>"实现复数类数据的输入与输出。
class Complex9
{
double rpart,ipart;
public:
Complex9(double r=0.0,double i=0.0)
{
rpart=r;
ipart=i;
}
friend istream& operator>>(istream&,Complex9&);
friend ostream& operator<<(ostream&,Complex9&);
};
istream& operator>>(istream& is,Complex9& c)
{
is>>c.rpart>>c.ipart;
return is;
}
ostream& operator<<(ostream& os,Complex9& c)
{
os<<"("<<c.rpart<<","<<c.ipart<<")\n";
return os;
}
void main16_9()
{
Complex9 a;
cout<<"Please input two Complex9 number:\n";
cin>>a;
cout<<a;
}
//【例16.10】 利用ios类中的设置域宽、填充字符和浮点数精度来控制数据输出格式。
void main16_10()
{
double d=1234.56789,c=98.76;
cout.setf(ios::right|ios::showpoint|ios::fixed);
//设置右对齐,对浮点数要显示小数点,按定点数形式显示浮点数
cout.precision(8); //设置浮点数显示精度为小数点后8位
cout.fill('*'); //设置填充字符为'*'
cout.width(18); //设置下一个输出数据的数据域宽为18位
cout<<d<<endl;
cout.width(18); //设置下一个输出数据的数据域宽为18位
cout<<c<<endl;
cout.setf(ios::hex);
cout<<15<<endl;
cout.setf(ios::oct);
cout<<15<<endl;
}
//【例16.11】 利用ios类中的流操纵算子进行输入/输出。
#include <iomanip.h>
void main16_11()
{
int i=32;double x=2.3456789;
cout<<"decimal is "<<setiosflags(ios::showbase)<<setfill('*')
<<setw(6)<<i<<endl;
//设置输出数据前带有基数符、填充符为'*'、域宽为6,默认十进制
cout<<"octal is "<<oct<<setw(6)<<i<<endl; //数据输出为八进制
cout<<"hexadecimal is "<<hex<<setw(6)<<i<<endl; //数据输出为十六进制
cout<<setprecision(3)<<x<<endl;
}
//【例16.12】 向文本文件中写入一个字符串和一个整型数。
#include <fstream.h>
void main16_12()
{
ofstream fos; //定义输出文件流对象fos
ifstream fis; //定义输入文件流对象fis
char str[80]="";
int num;
fos.open("file.txt"); //建立fos与文件fiel.txt的关联
fos<<"Windows"<<endl;
fos<<98<<endl;
fos.close(); //关闭流对象fos
fis.open("file.txt"); //建立fis与文件fiel.txt的关联
fis>>str>>num;
cout<<str<<","<<num<<endl;
fis.close(); //关闭流对象fis
}
//【例16.13】 已知磁盘文件file1.txt,将该文件的内容拷贝到另一个文件file2.txt中。
#include <fstream.h>
#include <stdlib.h>
void main16_13()
{
fstream fin,fout;
char ch;
fin.open("file1.txt",ios::in);
if (!fin) //若文件打开失败,则告警出错返回
{
cout<<"Error:file1.txt can't open!\n";
exit(1);
}
fout.open("file2.txt",ios::out);
if (!fin)
{
cout<<"Error:file2.txt can't open!\n";
exit(1);
}
ch=fin.get(); //或 fin>>ch; 但不能读取文件中的换行符和空格
while(!fin.eof()) //判断是否到文件尾
{
fout.put(ch); //或 fout<<ch;
ch=fin.get(); // 或fin>>ch;
}
fin.close();
fout.close();
}
//【例16.14】 设文本文件data.txt中有若干个实数,每一个实数之间用空格或换行符隔开。求出文件中的这些数的平均值。
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
void main16_14(void)
{
ifstream infile("data.txt",ios::in|ios::nocreate);
//以输入方式打开,且文件不存在是不能自动创建文本
if(!infile)
{
cout<<"Error:file data.txt can't open!\n";
exit(1);
}
float sum=0,temp;
int count=0;
while(infile>>temp) //不能用infile.get(temp)
{
sum+=temp;
count++;
}
cout<<"The average is "<<sum/count<<'\t'<<"count="<<count<<'\n';
infile.close();
}
//【例16.15】 将几个学生的基本信息写入二进制文件students.txt中,并将该文件中的信息读出来,分别显示在屏幕上。
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
class Student //定义一个反映学生基本信息的student类
{
public:
char name[20]; //姓名
double height; //身高
unsigned short age; //年龄
double score; //成绩
Student() //不带参数的构造函数
{
strcpy(name,"");
height=0;
age=0;
score=0;
}
Student(char* sn,double sh,unsigned short sa,double ss)
{
strcpy(name,sn); //带参数的构造函数
height=sh;
age=sa;
score=ss;
}
};
void main16_15()
{
Student stud[3];
ofstream fout; //fout输出文件流对象
ifstream fin; //fin输入文件流对象
int i=0;
fout.open("students.txt",ios::out|ios::binary);
if (!fout)
{
cout<<"Error:Can't open students.txt!";
exit(1);
}
stud[0]=Student("Wang",1.70,20,85.5);
stud[1]=Student("Li",1.65,21,92.3);
stud[2]=Student("Hu",1.84,19,86.7);
while(fout&&i<3)
{
fout.write((char*)&stud[i],sizeof(Student));
i++; //将学生信息Student类对象写入文件
}
fout.close();
fin.open("students.txt",ios::in|ios::binary);
if (!fin)
{
cout<<"Error:Can't open students.txt!";
exit(1);
}
cout<<"Name\t"<<"Height\t"<<"Age\t"<<"Score\n";
i=0; //"\t"为制表符,以控制信息纵向对齐
while(fin&&i<3)
{
fin.read((char*)&stud[i],sizeof(Student));
//从文件中读入Student类对象内容
cout<<stud[i].name<<"\t"<<stud[i].height<<"\t"
<<stud[i].age<<"\t"<<stud[i].score<<"\n";
i++;
}
fin.close();
}
//【例16.16】 利用ostrstream类流将浮点数转化为字符串输出。
#include <strstrea.h>
void main16_16()
{
const int LENGTH=80;
char buffer[LENGTH]; //定义字符缓冲区buffer
double dnum=+1234.5678;
ostrstream sout(buffer,sizeof(buffer));
//定义字符串输出流对象sout,与缓冲区buffer相联
sout.setf(ios::fixed|ios::showpoint|ios::showpos);
sout<<"This value of dnum="<<dnum<<'\0';
cout<<buffer<<endl; //输出字符串数组buffer内容
cout<<"The Length of buffer is "<<sout.pcount()<<endl;
//输出流对象缓冲区的字符长度
char *p=sout.str(); //缓冲区的首地址赋值给指针变量p
cout<<p<<endl; //输出缓冲区的内容
}
//【例16.17】 利用istrstream类流将字符串的数字字符转换为对应的数字进行运算。
#include <strstrea.h>
void main()
{
const int LENGTH=80;
char buffer1[LENGTH]="65 43.21";
char buffer2[LENGTH]="543.21";
int i;
double d;
istrstream s1(buffer1); //定义字符串输入流s1,与缓冲区buffer1全部字符相联
s1>>i>>d;
cout<<i+d<<endl;
istrstream s2(buffer2); //定义字符串输入流s2,与缓冲区buffer2全部字符相联
istrstream s3(buffer2,3); //定义字符串输入流s3,与缓冲区buffer2前三个字符相联
s2>>d; //将缓冲区中buffer2的全部数字字符转换浮点数赋值给d
s3>>i; //将缓冲区中buffer2的前三个数字字符转换整型数赋值给i
cout<<i+d<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -