C++io流

流:数据从一段到另一段

运算符:<<        >>

cout 标准输出

#include <iostream>
using namespace std;


int main()
{
	cout << "标准输出";


	return 0;
}

cin 标准输入

#include <iostream>
using namespace std;


int main()
{
	int a = 10;
	cin >> a;


	return 0;
}

cerr 标准报错

#include <iostream>
using namespace std;


int main()
{
	cerr << "标准报错";

	return 0;
}

clog 标准日志

#include <iostream>
using namespace std;

int main()
{
	clog << "标准日志";

	return 0;
}

格式控制

头文件<iomainp>

设置精度setprecision

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

int main()
{
	cout << setprecision(4) << 3.1415926;//设置精度

	return 0;
}

设置保留小数

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

int main()
{
	cout <<fixed<<setprecision(4) << 3.1415926;//设置保留小数

	return 0;
}
#include <iostream>
#include<iomanip>
using namespace std;

int main()
{
	cout.put('a');//打印单个字符
	cout.write("eeeee", 3);//截取
	cout << endl;//换行

	return 0;
}

字符流

头文件:<sstream>

    stringstream  可读可写
    istringstream  只能读不能写
    ostringstream  只能写不能读

#include <iostream>
#include<iomanip>
#include<sstream>
using namespace std;

int main()
{
	stringstream s1;//可读可写
	istringstream s2;//只能读不能写
	ostringstream s3;//只能写不能读

	return 0;
}
#include <iostream>
#include<iomanip>
#include<sstream>
using namespace std;

int main()
{
	stringstream s1;//可读可写
	s1 << "hello";
	cout << s1.str() << endl;//获取str
	s1.clear();//重置
	string b;
	s1 >> b;

	return 0;
}

文件流

头文件<fstream>

    fstream 可读可写
    ifstream 可读不可写
    ofstream 可写不可读

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

int main()
{
	
	fstream f1;//可读可写
	ifstream f2;//可读不可写
	ofstream f3;//可写不可读

	return 0;
}

写文件

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

int main()
{
	
	fstream f1;//可读可写
	f1.open("1.txt", ios::out);
	f1 << "写文件,写完一定要关闭";
	f1.close();//关闭


	return 0;
}

二进制写文件

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

int main()
{
	
	fstream f1;//可读可写
	f1.open("1.txt", ios::out|ios::binary);
	f1.write("二进制写文件写完一定要关闭", 1024);
	f1.close();


	return 0;
}

读文件

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

int main()
{
	
	fstream f1;//可读可写
	f1.open("1.txt", ios::in);
	char a[1024];
	f1 >> a;
	cout << a << endl;


	return 0;
}
  • ios::beg 文件开始位置 //SEEK_SET

  • ios::end 文件结束位置

  • ios::cur 文件当前位置

  • 打开方式

  • os::in 读的方式打开文件

  • ios::out 写方式打开文件 具有创建功能

  • ios::app 追加模式 具有创建功能呢

  • ios::ate 追加模式,文件指针指向末尾

  • ios::trunc 具备创建功能

  • ios::nocreate 不具备创建

  • ios::noreplace 不替换 (想想C语言 w方式)

  • ios::binary 二进制的形式

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值