17、面向对象-io操作

一、I/O流概念和流类库结构

1、概念

 C++输入输出包含以下三个方面的内容:
    对系统指定的标准设备的输入和输出。即从键盘输入数据,输出到显示器屏幕。这种输入输出称为标准的输入输出,简称标准I/O
    以外存磁盘文件为对象进行输入和输出,即从磁盘文件输入数据,数据输出到磁盘文件。以外存文件为对象的输入输出称为文件的输入输出,简称文件I/O
    对内存中指定的空间进行输入和输出。通常指定一个字符数组作为存储空间(实际上可以利用该空间存储任何信息)。这种输入和输出称为字符串输入输出,简称串I/O

2、与iostream类库有关的头文件

iostream  包含了对输入输出流进行操作所需的基本信息。
fstream  用于用户管理的文件的I/O操作。
strstream  用于字符串流I/O。
stdiostream  用于混合使用C和C + +的I/O机制时,例如想将C程序转变为C++程序。
iomanip  在使用格式化I/O时应包含此头文件。

二、标准IO流

1、标准I/O对象:cin,cout,cerr,clog

2、标准输入流

标准输入流对象cin,重点掌握的函数
      cin.get() //一次只能读取一个字符  遇到EOF结束,将刷新缓冲区
      cin.get(一个参数) //读一个字符
      cin.get(两个参数) //可以读字符串,后参数为长度
      cin.getline()
      cin.ignore()
      cin.peek()       //复制输入缓冲区的内容,不会刷新缓冲区
      cin.putback()   //放回缓冲区

#include <iostream>

using namespace std;

int main()
{
	char ch;
	char buf[32] = {0};

	/*cin >> ch;
	cout << ch << endl;*/

	/*ch = cin.get();
	cout << ch << endl;*/

	/*cin.get(ch);
	cout << ch << endl;*/

	//cin.get(buf,10);     //最多获取10个字节
	//cin >> buf;
	//cout << buf << endl;
	
	//cin.getline(buf,10);        //获取一行数据,最多10字节
	//cout << buf << endl;
	
	/*cin.ignore(5);    //忽略前五个字节,然后在获取
	cin >> buf;
	cout << buf << endl;*/

	/*ch = cin.peek();   //获取一个字符,同时字符还留在缓冲区
	cout << ch << endl;

	cin >> buf;
	cout << buf << endl;*/

	cin >> ch;
	cin.putback(ch);       //把ch放回缓冲区
						   //
	cin >> buf;
	cout << buf << endl;

	return 0;
}

3、标准输出流

标准输出流对象cout
      cout.flush()
      cout.put()
      cout.write()
      cout.width()
      cout.fill()
      cout.setf(标记)
 
manipulator(操作符、控制符)
flush
endl
oct
dec
hex
setbase
setw
setfill
setprecision

4、C++格式化输出,C++输出格式控制

1、使用控制符方法

int main()
{
	//使用控制符的方法
	int num = 1000;
	cout << oct << num << endl;   //输出八进制
	cout << dec << num << endl;
	cout << hex << num << endl;
	cout << setbase(8) << num << endl;
	
	double PI = 222.0000 / 7.00000;
	cout << PI << endl;	
	cout << setprecision(15) << PI << endl;        //控制小数输出位数
	
	cout << setprecision(5) << setiosflags(ios::scientific) << PI << endl;    //科学计数法
	
	const char *s = "helloworld";
	cout << setw(15) << setfill('*') << s << endl;       //右对其输出15个字符,空格用*填充

	return 0;
}

2、成员函数来控制

1、需要结束当前的输出格式

2、开启需要的输出格式

	//使用成员函数的方式
	int num = 1000;
	cout.unsetf(ios::dec);         //结束十进制格式输出
	cout.setf(ios::oct);          //设置八进制格式输出
	cout << num << endl;

	cout.unsetf(ios::oct);
	cout.setf(ios::hex);
	cout << num << endl;
	
	double PI = 222.000 / 7.000;

	cout.precision(5);
	cout << PI << endl;
	cout.setf(ios::scientific);
	cout << PI << endl;

	const char *s = "helloworld";
	cout.width(15);
	cout.fill('^');
	cout << s << endl;

三、文件IO

1、写文件(相对于程序而言,输出流对象,从程序输出到文件)

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	char buf[32] = "helloworld";
	ofstream ofs;     //创建输出流对象 写文件
	ofs.open("hello.txt",ios::out);     //使用默认参数,默认是输出格式 ios::out
	
	ofs << buf;                 //使用输出运算符,把buf写入文件buf>>ofs  不可能这样使用 >>输入运算符
	ofs.close();

	return 0;
}

2、读文件(相对于程序而言,输入流对象,从文件输入到程序)

memset(buf,0,sizeof(buf));
	ifstream ifs("hello.txt");   //通过构造函数,打开文件   输入流对象,就是在读文件
	ifs >> buf;
	cout << buf << endl;
	ifs.close();

3、读写文件(fstream的对象要指定输入、输出流)

	char buf[32] = "helloworld";
	fstream fs("hello.c",ios::out);   //输出流,写文件,自动创建文件
	//fs.write(buf,strlen(buf));
	fs << buf;
	fs.close();

	memset(buf,0,sizeof(buf));
	fs.open("hello.c",ios::in);
	fs >> buf;
	cout << buf << endl;

一个一个读

	char ch;
	memset(buf,0,sizeof(buf));
	fs.open("hello.c",ios::in);
	while((ch = fs.get()) != EOF)            //没有到文件末尾
	//fs >> buf;
	cout << ch;

	cout << endl;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值