41 C++输入输出流

1 标准输入流函数

标准输入流对象cin,重点掌握的函数

cin.get(一个参数) //读一个字符
cin.get(三个参数) //可以读字符串
cin.getline()
cin.ignore()
cin.peek()
cin.putback()

#include<iostream>
using namespace std;

//标准输入流对象cin,重点掌握的函数
//cin.get() //一次只能读取一个字符
//cin.get(一个参数) //读一个字符
//cin.get(三个参数) //可以读字符串
//cin.getline()
//cin.ignore()
//cin.peek()
//cin.putback()

void main() {
	char mybuf[1024];
	int myInt;
	long myLong;
	cin >> myInt;
	cin >> myLong;
	cin >> mybuf;	//遇见空格停止接收数据

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

cin.get() //一次只能读取一个字符 输入EOF时(ctrl+z)结束, 回车从缓冲区输出字符

void main() {
	char ch;
	while ((ch = cin.get()) != EOF) {
		cout << ch << endl;
	}
}

结果

abcdef
a
b
c
d
e
f


^Z

缓冲区

void main() {
	char a, b, c;
	cout << "cin.get(a) 如果缓冲区没有数据,则程序阻塞\n";
	cin.get(a);
	cin.get(b);
	cin.get(c);

	cout << a << b << c << "因为缓冲区有数据,所以程序不会阻塞\n";
	cin.get(a).get(b).get(c);
	cout << a << b << c;
}

结果

cin.get(a) 如果缓冲区没有数据,则程序阻塞
abcdefg
abc因为缓冲区有数据,所以程序不会阻塞
def

getline

// getline可以接收空格
void main() {
	char buf1[256];
	char buf2[256];
	cout << "请输入一个字符串含有多个空格 aa cc dd\n";

	cin >> buf1;

	cin.getline(buf2, 256);
	cout << "buf1:" << buf1 << "\nbuf2:" << buf2 << endl;
}

结果

请输入一个字符串含有多个空格 aa cc dd
aacc bb aa ee
buf1:aacc
buf2: bb aa ee

ignore 忽略缓冲区数据

void main() {
	char buf1[256];
	char buf2[256];
	cout << "请输入一个字符串含有多个空格 aa  bbccdd\n";

	cin >> buf1;
	cin.ignore(2);

	cin.getline(buf2, 256);
	cout << "buf1:" << buf1 << "\nbuf2:" << buf2 << endl;
}

结果

请输入一个字符串含有多个空格 aa  bbccdd
aa  bbccdd
buf1:aa
buf2:bbccdd

peek 看缓冲区中的数据

void main() {
	char buf1[256];
	char buf2[256];
	cout << "请输入一个字符串含有多个空格 aa  bbccdd\n";

	cin >> buf1;
	cin.ignore(2);
	int myint = cin.peek();
	cout << "myint:" << myint << endl;

	cin.getline(buf2, 256);
	cout << "buf1:" << buf1 << "\nbuf2:" << buf2 << endl;
}

结果

请输入一个字符串含有多个空格 aa  bbccdd
aa bde
myint:100
buf1:aa
buf2:de

案例:输入的整数和字符串分开处理

//案例:输入的整数和字符串分开处理
int main()
{
	cout << "Please, enter a number or a word: ";
	char c = std::cin.get();

	if ((c >= '0') && (c <= '9')) //输入的整数和字符串 分开处理
	{
		int n; //整数不可能 中间有空格 使用cin >>n
		cin.putback(c);
		cin >> n;
		cout << "You entered a number: " << n << '\n';
	}
	else
	{
		string str;
		cin.putback(c);
		getline(cin, str); // //字符串 中间可能有空格 使用 cin.getline();
		cout << "You entered a word: " << str << '\n';
	}	system("pause");
	return 0;
}

2 标准输出流函数

void main() {
	cout << "hello" << endl;
	cout.put('h').put('e').put('o').put('\n');

	const char* p = "hello idcast";
	cout.write(p, strlen(p)) << endl;
	cout.write(p, strlen(p) - 4) << endl;
	cout.write(p, strlen(p) + 4) << endl;

}

结果

hello
heo
hello idcast
hello id
hello idcast
#include<iostream>
#include<string>
#include <iomanip> 
using namespace std;
void main() {
	//使用类成员函数
	cout << "<start>";
	cout.width(30);
	cout.fill('*');
	cout.setf(ios::showbase); //#include <iomanip>
	cout.setf(ios::internal); //设置
	cout << hex << 123 << "<End>\n";

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

}

结果

<start>0x**************************7b<End>


<Start>0x**************************7b<End>

3 文件I/O

1 文件读写

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

void main() {
	// 写文件
	const char* fname = "1.txt";
	ofstream fout(fname);	//建立输出流和文件进行关联
	fout << "hello...1" << endl;
	fout << "hello...2" << endl;
	fout << "hello...3" << endl;
	fout.close();

	// 读文件
	ifstream fin(fname);	//建立输入流和文件关联
	char ch;
	while (fin.get(ch)) {
		cout << ch;
	}
	fin.close();
}

得到1.txt文件

2 二进制文件读写

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<fstream>
class Teacher {
private:
	int age;
	char name[32];
public:
	Teacher() {
		age = 33;
		strcpy(name, "");
	}
	Teacher(int _age, const char* _name) {
		age = _age;
		strcpy(name, _name);
	}
	void printT() {
		cout << "age: " << age << " name: " << name << endl;
	}
};

void main() {
	const char* fname = "2.dat";
	ofstream fout(fname, ios::binary);
	if (!fout) {
		cout << "打开文件失败" << endl;
		return;
	}
	Teacher t1(31, "t31"), t2(32, "t32");
	fout.write((char*)& t1, sizeof(t1));
	fout.write((char*)& t2, sizeof(t2));
	fout.close();

	//
	ifstream fin(fname);
	Teacher tmp;

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

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

	fin.close();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值