C++ 标准输入输出流

标准输入流

#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>

using namespace std;

/*
cin.get() //一次只能读取一个字符
cin.get(一个参数) //读一个字符
cin.get(三个参数) //可以读字符串
cin.getline()
cin.ignore()
cin.putback()
*/

//cin的operator>>操作符 //根据回车来刷新缓冲区  根据空格来隔离每个变量的内容
void test1()
{
	int myInt;
	long myLong;

	char buf[128] = { 0 };

	cin >> myInt;
	cin >> myLong;
	cin >> buf;

	cout << "myInt: " << myInt << endl;
	cout << "myLong: " << myLong << endl;
	cout << "buf: " << buf << endl;
}


//cin.get()方法
void test2()
{
	char ch;

	//cin.get()如果读到的不是EOF标识符,那么会永远的阻塞等待
	//从键盘来讲ctrl+z 代表EOF标识符
	while ((ch = cin.get() )!= EOF) {
		cout << ch << endl;
	}
}

void test3()
{
	char a, b, c;

	char buf[10] = { 0 };

	cout << "从输入缓冲区去读取数据,如果缓冲区中没有数据,就阻塞" << endl;
	//cin.get(a); //从输入缓冲区去读取数据,如果有就给a
	//cin.get(b);
	//cin.get(c);
	//cin.get(a).get(b).get(c);


	//cout << "a  =" << a << endl;
	//cout << "b  =" << b<< endl;
	//cout << "c  =" << c<< endl;

	cin.get(buf, 10, ' ');

	cout << buf << endl;
}

//cin.getline()
void test4()
{
	char buf[128] = { 0 };
	cout << "请输入一个字符串 aa bb cc dd" << endl;
	cin.getline(buf, 128); //从输入缓冲区中读数据到buf中,最多读128 ,知道遇到\n为止

	cout << "buf:" <<buf << endl;
}


//cin.ignore()
void test5()
{
	char buf1[128];
	char buf2[128];

	cout << "请输入一个字符串 aa  bb cc dd" << endl;
	cin >> buf1; //aa
	cin.ignore(2);
	cin.getline(buf2, 128);// bb cc dd

	cout << "buf1:" << buf1 << endl;
	cout << "buf2:" << buf2 << endl;

}

//cin.putback()
void test6()
{
	cout << "请输入一个数字或者字符串" << endl;
	char ch;
	ch = cin.get(); //从输入缓冲区去读一个字符
	if ((ch >= '0') && ch <= '9') {
		cout << "输入的是一个数字" << endl;
		int num;
		//此时数字第一个字符已经读出来了。 需要将ch放回到输入缓冲区
		cin.putback(ch); //将ch仍会缓冲区, 位置就缓冲区的头部。
		cin >> num;

		cout << "num =" << num << endl;
	}
	else {
		cout << "输入的是一个字符串" << endl;
		char buf[128] = { 0 };
		//cin.putback(ch);
		cin.getline(buf, 128);

		cout << "buf:" << buf << endl;
	}
}

int main(void)
{
	//test1();
	//test2();
	//test3();
	//test4();
	//test5();
	test6();
	
	return 0;
}

标准输出流

#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>
#include	<iomanip>

using namespace std;

/*
cout.put()
cout.write() //向输出缓冲区中写数据
cout.width()
cout.fill()
cout.setf(标记)


操作符、控制符
flush
endl
oct
dec
hex
setbase
setw
setfill
setprecision
*/

void test1()
{
	cout << "hello " << endl;
	cout.put('h').put('e').put('l') << endl;

	char str[] = "hello wolrd";
	cout.write(str, strlen(str));
	cout << endl;
	cout.write(str, strlen(str) - 1);
	cout << endl;
}


void test2()
{
	/*
	//使⽤类成员函数
	cout << "<start>";
	cout.width(30); //设置接下来要输出的长度,是30
	cout.fill('*'); //将没有填充的多余的空间,填充成*
	cout.setf(ios::showbase);	//#include	<iomanip>
	cout.setf(ios::internal);	//设置
	cout << hex << 123 << "<End>\n";
	cout << endl;
	*/

	
	//使⽤ 操作符、控制符
	cout << "<Start>"
		<< setw(30)
		<< setfill('*')
		<< setiosflags(ios::showbase)	//基数
		<< setiosflags(ios::internal)
		<< hex
		<< 123
		<< "<End>\n"
		<< endl;

}

void test3()
{
#if 0
	int	a;
	cout << "input	a:";
	cin >> a;
	cout << "dec:" << dec << a << endl;	//以⼗进制形式输出整数
	cout << "hex:" << hex << a << endl;	//以⼗六进制形式输出整数a
	cout << "oct:" << setbase(8) << a << endl;	//以⼋进制形式输出整数a

	const char	*pt = "China";	//pt指向字符串"China"
	cout << setw(10) << pt << endl;	//指定域宽为,输出字符串
	cout << setfill('*') << setw(10) << pt << endl;	//指定域宽,输出字符串,空⽩处以'*'填>充
#endif

	double	pi = 22.0 / 7.0;	//计算pi值
	//按指数形式输出,8位⼩数
	cout << setiosflags(ios::scientific) << setprecision(8);
	cout << "pi=" << pi << endl;	//输出pi值
	cout << "pi=" << setiosflags(ios::fixed) << pi << endl;	//改为⼩数形式输出

	//cout << "pi=" << setprecision(4) << pi << endl;	//改为位⼩数
}

void test4()
{
	double a = 123.456, b = 3.14159, c = -3214.67;
	cout << setiosflags(ios::fixed) << setiosflags(ios::right) << setprecision(2);
	cout << setw(10) << a << endl;
	cout << setw(10) << b << endl;
	cout << setw(10) << c << endl;

	//       123.45
	//          3.14
	//    -3214.67
}
int main(void)
{
	
	//test1();
	//test2();
	//test3(); 
	test4();
	return 0;
}

C++ 文件流的基本操作

#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>
#include <fstream>

using namespace std;

void test_write_to_file()
{
	//需要创建一个文件的输出流cout ostream,  fout ofstream
	char file_name[] = "c:/test/hahaha.txt";
	//strcpy(file_name, "c:/test/hahaha.txt");

	ofstream fout(file_name, ios::app | ios::out); //如果不写这个打开文件的控制符,默认是ios::out
	if (!fout) {
		cout << "打开文件失败" << endl;
	}

	fout << "hello...111" << endl;
	fout << "hello...222" << endl;
	fout << "hello...333" << endl;


	fout.close();
}

void test_read_from_file()
{
	char file_name[] = "c:/test/hahaha.txt";

	ifstream fin(file_name, ios::in); //ios::in
	if (!fin) {
		cout << "打开文件失败" << endl;
	}
	char ch;
	while (fin.get(ch)) {
		cout << ch;
	}

	fin.close();
}


class Teacher
{
public:
	Teacher() {

	}
	Teacher(int id, const char name[])
	{
		this->id = id;
		strcpy(this->name, name);
	}
	void printT() {
		cout << "id = " << id << endl;
		cout << "name = " << name << endl;
	}
private:
	int id;
	char name[32];
};

void test_write_binary_to_file()
{
	char file_name[] = "c:/test/bin_teacher.dat";
	Teacher t1(1001, "zhang3");
	Teacher t2(1002, "li4");

	ofstream fout(file_name, ios::binary);
	if (!fout) {
		cout << "打开文件失败" << endl;
		return;
	}

	fout.write((char*)&t1, sizeof(Teacher));
	fout.write((char*)&t2, sizeof(Teacher));

	fout.close();
}

void test_read_binary_from_file()
{
	char file_name[] = "c:/test/bin_teacher.dat";
	Teacher temp1;
	Teacher temp2;


	ifstream fin(file_name, ios::binary);
	if (!fin) {
		cout << "打开文件失败" << endl;
		return;
	}

	fin.read((char*)&temp1, sizeof(Teacher));
	temp1.printT();
	fin.read((char*)&temp2, sizeof(Teacher));
	temp2.printT();

	fin.close();
}

int main(void)
{
	//test_write_to_file();
	//test_read_from_file();
	//test_write_binary_to_file();
	test_read_binary_from_file();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

banjitino

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值