C++IO流

流:诺干字节数据从一端到另一端

流类体系:

1.流对象

2.流运算符 <<  >>

输入输出流

1.ostream 类 

(1)cout 可重定向为文件

(2)cerr 不能重定向为文件

(3)clog 可重定向为文件

(4)字符类的处理

(5)格式控制字符:#include  <iomanip>

   *设置有效位数:setprecision(int)

   *设置精度:fix结合setprecision(int)

2.istream 类 cin

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	//字符
	cout.put('A');       //A
	cout << 'A' << endl; //A
	char B = 'B';        
	cout.put(B);         //B
	//字符串
	cout.write("Iloveyou", 5);//Ilove
	//字符输入
	cout.put(cin.get());//B=cin.get();cout.put(B);
	//清空缓存区
	while (cin.get() != '\n');//while(getchar()!='\n');
	//字符串输入
	char str[20]="";
	cin.getline(str, 20);
	cout.write(str, 20);
	//格式控制字符
	double p = 3.1415926;
	cout << setprecision(3) << p << endl;//3.14
	cout << fixed << setprecision(6) << p << endl;//3.141593
	//进制输出
	cout << hex << 32 << endl; //16进制
	cout << oct << 32 << endl; //8进制
	cout << setbase(8) << 32 << endl;//8 10 16进制
	//制表
	cout << setw(8) << "ABC" << setw(16) << "DEF" << setw(32) << "GHI" <<endl;
	//右对齐
	cout << setiosflags(ios::right);//setiosflags(ios::left)左对齐
	cout << "ABC" << setw(8) << "DEF" << setw(16) << "GHI" << setw(32);
	return 0;
}

字符流

1.#include <sstream>

(1)istringstream 类

(2)ostringstream 类

(3)stringstream 类(一般使用)

2.获取字符流对象中的数据

(1)string str()  //获取string

(2)void str(const string& str); //重新设置字符流对象的数据

3.一般字符流对象做字符串处理

(1)字符串的分割

(2)字符串转换问题

4.一个流多次做数据操作,要做clear操作

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
	stringstream sso(string("Iloveyou"));
	cout << sso.str() << endl;
	stringstream ssn;
	ssn << "Iloveyou";
	cout << ssn.str() << endl;
	string data;
	ssn<<data;
	cout << data << endl;
	//数字转字符
	int num = 1234;
	char str[20] = "";
	stringstream ssc;
	ssc << num;
	ssc >> str;
	cout << str << endl;
	//字符转数字
	stringstream ssa("1234");
	int temp;
	ssa >> temp;
	cout << temp << endl;
	//分割
	stringstream sss("11,22,33,44,55,66,77,88,99");
	int arr[9];
	char c[8];
	for (int i = 0; i < 9; i++)
	{
		if (i==8)
		    sss >> arr[i];
		else
			sss >> arr[i] >> c[i];
	}
	for (int i = 0; i < 9; i++)
	{
		cout << arr[i] << "\t";
	}
	//clear操作
	ssc.clear();
	ssc << num;
	ssc >> str;
	cout << str << endl;
	return 0;
}

文件流

#include <fstream>

1.流类体系

(1)ofstream 类  写操作  output输出到文件

(2)ifstream 类  读操作

(3)fstream 类  可读可写

2.打开关闭文件

(1)打开文件:void  open  (const  char*  URL,ios::openmode  mode);

(2)读写方式

*ios::in  读

*ios::out  写,创建

*ios::app  追加,创建

*ios::ate  追加,文件指针指向末尾

*ios::trunc  创建

*ios::nocreate  

*ios::noreplace  不替换

*ios::binary  二进制

*读写组合:*ios::in|*ios::out|*ios::binary

(3)关闭文件:void  close( )

3.判断是否打开成功

(1)文件流对象重载运算

(2)is_open成员函数判断

4.读写文件

(1)流的方式读写

(2)二进制方式读写

5.文件指针定位

(1)ifstream类的对象

*istream& seekg(long int pos);

*istream& seekg(long int pos,ios::seekdir position);

(2)ofstream类的对象

*ostream& seekp(long int pos);

*ostream& seekp(long int pos,ios_base::seekdir position);

(3)ios_base::seekdir

*ios::beg  文件开始位置

*ios::end  文件结束位置

*ios::cur  文件当前位置

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
void rwfile(const char* rfilename, const char* wfilename)
{
	fstream rfn(rfilename, ios::in);
	fstream wfn(wfilename, ios::out);
	1.流的方式,会忽略空格和换行
	//while (1)
	//{
	//	char a;
	//	rfn >> a;
	//	if (rfn.eof())break;
	//	wfn << a;
	//}
	2.成员函数,不会忽略空格和换行
	//while (1)
	//{
	//	char a;
	//	rfn.get(a);
	//	if (rfn.eof())break;
	//	wfn.put(a);
	//}
	//3.字符串
	while (!rfn.eof())
	{
		char str[1024] = "";
		rfn.getline(str,1024);
		wfn.write(str, strlen(str));
	}
	rfn.close();
	wfn.close();
}
void binaryrwfile(const char* rfilename, const char* wfilename)
{
	//二进制读写
	fstream rfn(rfilename, ios::in|ios::binary);
	fstream wfn(wfilename, ios::out|ios::binary);
	while (!rfn.eof())
	{
		char str[1024] = "";
		rfn.read(str, 1024);
		wfn.write(str, 1024);
	}
	rfn.close();
	wfn.close();
}
void seekfile(const char* filename)
{
	fstream fread(filename, ios::in);
	if (!fread)
		cout << "打开失败" << endl;
	fread.seekg(4);
	char a = fread.get();
	cout << a;
	fread.seekg(-4, ios::end);
	a = fread.get();
	cout << a;
	fread.close();
}
int main()
{
	rwfile("read.txt", "write.txt");
	binaryrwfile("read.txt", "write.txt");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值