其中,'f’代表该类对文件操作(读\写),'str’代表该类对内存操作(读\写)
定义:通过调用 I/O对象的成员函数,改变或者获取其中的状态,进而影响格式化数据的形式(格式化函数是类的共有成员函数)
例如:
int ios::precision(int);//控制浮点数的输出精度
int ios::width(int);//控制显示的域宽
格式化函数举例:
#include //iostream类是istream类、ostream类的子类
#include
using namespace std;
int main(void){
cout << "sqrt(109) = " << sqrt(109) << endl;
cout << "cout.precision(10)" << endl;
cout.precision(10);//使用继承自ios基类的格式控制函数,控制输出精度
cout << "sqrt(109) = " << sqrt(109) << endl;
/************/
cout << "cout.setf(ios::scientific)" << endl;
cout.setf(ios::scientific);//通过格式控制函数,控制输出的形式为科学计数法
cout << "sqrt(109) = " << sqrt(109) << endl;
/************/
cout << '[';
cout.width(10);//控制域宽为10个字符
cout.fill('*');//使用‘*’填充空白
cout.setf(ios::showpos);//设置正数显示‘+’符号
cout << 517;
cout << ']' << endl;
return 0;
}
编译输出结果:
Desktop Linraffe$ g++ io.cpp -o io
Desktop Linraffe$ io
sqrt(109) = 10.4403
cout.precision(10)
sqrt(109) = 10.44030651
cout.setf(ios::scientific)
sqrt(109) = 1.0440306509e+01
[******+517]
流控制符定义:通过将流控制符插入到输入/输出流中,改变其中状态,进而影响格式化数据形式(流控制符是全局函数,可以实现和格式化函数相同的功能)
如:
serprecision(int)//和int ios::precision(int);语句实现的功能相同:控制浮点数的输出精度
setw(int)//和int ios::width(int);语句实现的功能相同:控制字符的域宽
流控制符举例:
#include //iostream类是istream类、ostream类的子类
#include
#include //包含带参数的流控制符
using namespace std;
int main(void){
cout << "sqrt(109) = " << sqrt(109) << endl;
cout << "cout.precision(10)" << endl;
cout.precision(10);//使用继承自ios基类的格式控制函数,控制输出精度
cout << "sqrt(109) = " << sqrt(109) << endl;
cout << "cout << setprecision(10) << sqrt(109) << endl:";
cout << setprecision(10) << sqrt(109) << endl;
cout << endl;
/************/
cout << "cout.setf(ios::scientific)" << endl;
cout.setf(ios::scientific);//通过格式控制函数,控制输出的形式为科学计数法
cout << "sqrt(109) = " << sqrt(109) << endl;
cout << "cout << scientific << sqrt(109) << endl:";
cout << scientific << sqrt(109) << endl;
cout << endl;
/************/
cout << '[';
cout.width(10);//控制域宽为10个字符
cout.fill('*');//使用‘*’填充空白
cout.setf(ios::showpos);//设置正数显示‘+’符号
cout << 517;
cout << ']' << endl;
cout << "cout << '['<< setw(10) << setfill('*') << showpos << 517 << ']' << endl:";
cout << '[' << setw(10) << setfill('*') << showpos << 517 << ']' << endl;;
return 0;
}
编译执行结果:
Desktop Linraffe$ g++ io.cpp -o io
Desktop Linraffe$ io
sqrt(109) = 10.4403
cout.precision(10)
sqrt(109) = 10.44030651
cout << setprecision(10) << sqrt(109) << endl:10.44030651
cout.setf(ios::scientific)
sqrt(109) = 1.0440306509e+01
cout << scientific << sqrt(109) << endl:1.0440306509e+01
[******+517]
cout << '['<< setw(10) << setfill('*') << showpos << 517 << ']' << endl:[******+517]
向内存读写操作时,所需头文件以及类
头文件:<sstream>
从内存读:istringstream类
向内存写:ostringstream类
stringstream类:istringstream类、ostringstream类的子类
举例:
#include //iostream类是istream类、ostream类的子类
#include
using namespace std;
int main(void){
char ca_name[10] = "giraffe";
int weight = 135;
double height = 178.8;
ostringstream oss;//定义一个对内存输入的对象
oss << ca_name << " is " << weight << "g," << height << "cm.";//向内存写操作
//等价于C语言中:
//char buf[1024];
//sprintf(buf, %s, %d, %lf,ca_name, weight, height);
string str_info = oss.str();
cout << str_info << endl;
cout << "********************" << endl;
istringstream iss;//创建一个从内存输出的对象
iss.str("unicorn 95 165");
iss >> ca_name >> weight >> height;
cout << "ca_name = " << ca_name << endl;
cout << "weight = " << weight << endl;
cout << "height = " << height << endl;
return 0;
}
编译输出结果:
Desktop Linraffe$ g++ io.cpp -o io
Desktop Linraffe$ io
giraffe is 135g,178.8cm.
********************
ca_name = unicorn
weight = 95
height = 165
文件流操作所需头文件以及类
头文件:<fstream>
读文件:ofstream类
写文件:ifstream类
fstream类:ofstream类、ifstream类的子类
举例:
#include //iostream类是istream类、ostream类的子类
#include
using namespace std;
int main(void){
ofstream ofs("zoo.txt");//创建文件输出流(向文件输出)对象,并初始化
ofs << "giraffe" << ' ' << 178.8 << ' ' << 135;//向文件输出
ofs.close();//关闭文件流,类似fopen后的close()
ifstream ifs("zoo.txt");//创建文件输入流(从文件输入)对象,并初始化
char ac_name[10];
double height;
int weight;
ifs >> ac_name >> height >> weight;
cout << "ac_name = " << ac_name << endl;
cout << "weight = " << weight <<endl;
cout << "height = " << height << endl;
ifs.close();
return 0;
}
编译执行结果:
Desktop Linraffe$ g++ io.cpp -o io
Desktop Linraffe$ io
ac_name = giraffe
weight = 135
height = 178.8
二进制的读写操作所需成员函数:
ostream& ostream::write(const char *buffer, size_t num)//类似fwrite
istream& istream::read(char buffer, streamsize num)//类似fread
举例:
#include //iostream类是istream类、ostream类的子类
#include
using namespace std;
int main(void){
ofstream ofs("zoo.txt");//创建文件输出流(向文件输出)对象,并初始化
char writeBuf[1024] = "giraffe、unicorn、fox、sheep、cat";
ofs.write(writeBuf,sizeof(writeBuf));
ofs.close();
ifstream ifs("file.txt");//创建文件输入流(从文件输出)对象,并初始化
char readBuf[1024] = {0};
ifs.read(readBuf,sizeof(readBuf));
cout << readBuf << endl;
ifs.close();
return 0;
}
编译执行结果:
Desktop Linraffe$ g++ io.cpp -o io
Desktop Linraffe$ io
giraffe、unicorn、fox、sheep、cat