C++基础学习(十)标准输入输出流

本文详细介绍了C++中的标准输入输出流,包括cin.get系列函数、输入流案例、cout设置格式以及文件的读写操作。通过示例展示了如何进行用户输入验证、输出格式控制以及如何使用ofstream和ifstream进行文件操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 标准的输入流

(1)cin.get 缓冲区中读取一个字符
(2)cin.get(两个参数) 不读换行符
(3)cin.getline()读取换行 并且扔掉
(4)cin.ignore(n) 忽略(n)n代表忽略字符数
(5)cin.peek() 偷窥 偷看1个字符然后放回去
(6)cin.putback 放回 把字符放回缓冲区

#include<iostream>
using namespace std;

void test()
{
	char c = cin.get();
	cout << c << endl;
}

void test01() 
{
	char buf[1024];
	cin.get(buf, 1024); //两个参数,可以读字符串
	char c = cin.get();
	if (c == '\n')
	{
		cout << "换行符还是缓存中" << endl;
	}
	else
	{
		cout << "换行符不在缓存汇总" << endl;
	}
}

void test02()
{
	char buf[1024];
	cin.getline(buf, 1024);
	char c = cin.get();
	if (c == '\n')
	{
		cout << "换行符还是缓存中" << endl;
	}
	else
	{
		cout << "换行符不在缓存汇总" << endl;
	}
}

void test03()
{
	cin.ignore(2);
	char c = cin.get();
	cout << "c=" << c << endl;
}

void test04()
{
	char a = cin.peek();
	char b = cin.get();
	cout << "a=" << a << " b=" << b << endl;
}

void test05()
{
	char a = cin.get();
	cin.putback(a);

	cout << "a=" << a << endl;
}

int main(void)
{
	//test();
	//test01();
	//test02();
	//test03();
	//test04();
	//test05();
	
	return EXIT_SUCCESS;
}

2 输入流案例

(1)判断用户输入的是字符还是数字 利用偷窥 或者放回
(2)让用户输入指定范围内的数字,如果不正确重新输入

  • cin.fail()看标志位,0正常 1 不正常
  • cin.clear() 重置表示位
  • cin.sync() 清空表示位

下面这个程序在vs2017上出错,但在codeblock上却是可以运行的。

#include<iostream>
using namespace std;

void test()
{
	int num;

	while (true)
	{
		cin >> num;
		if (num > 0 && num <= 10)
		{
			cout << "输入的数字为" << num << endl;
			break;
		}
		// 重置标志位
		cin.clear();// 将流中的所有状态值都重设为有效值
		cin.sync();//清空流

		cout << "标志位:" << cin.fail() << endl;
	}
}

int main(void)
{
	test();
	
	return EXIT_SUCCESS;
}

输出:

d
标志位:0
c
标志位:0
2
输入的数字为2

3 标准输出流

(1)流对象的成员函数

  • int number = 99;
  • cout.width(20);
  • cout.fill(’*’)
  • cout.setf(ios::left);
  • cout.unsetf(ios::dec);
  • cout.setf(ios::showbase);
  • cout.setf(ios::hex);
  • cout.setf(ios::showbase);

(2)控制符

  • int number = 99;
  • cout << setw(20) << setfill(’*’) << setiosflags(ios::showbase) << setiosflags(ios::left) << hex << number << endl;

4 文件读写操作

4.1 写文件

(1)ofstream ofs;
(2)open指定打开方式
(3)isopen判断是否打开成功
(4)ofs << “数据”
(5)ofs.close

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

// 写文件
void test()
{
	// 以输出的方式打开文件
	// ofstream ofs("./test.txt", ios::out|ios::trunc);
	// 后期指定打开方式
	ofstream ofs;
	ofs.open("./test.txt", ios::out | ios::trunc);
	if (!ofs.is_open())
	{
		cout << "打开失败!!!" << endl;
	}

	ofs << "姓名:小刘" << endl;
	ofs << "性别:男" << endl;
	ofs << "女朋友:暂无" << endl;
	ofs.close();
}

int main(void)
{
	test();

	// test01();
	
	return EXIT_SUCCESS;
}

输出:
在这里插入图片描述
在这里插入图片描述

4.2 读文件

(1)ifstream ifs;
(2)指定打开方式ios::in
(3)isopen判断是否打开成功
(4)三种方式读取数据

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

// 写文件
void test()
{
	// 以输出的方式打开文件
	// ofstream ofs("./test.txt", ios::out|ios::trunc);
	// 后期指定打开方式
	ofstream ofs;
	ofs.open("./test.txt", ios::out | ios::trunc);
	if (!ofs.is_open())
	{
		cout << "打开失败!!!" << endl;
	}

	ofs << "姓名:小刘" << endl;
	ofs << "性别:男" << endl;
	ofs << "女朋友:暂无" << endl;
	ofs.close();
}

// 读文件
void test01()
{
	ifstream ifs;
	ifs.open("./test.txt", ios::in);
	// 判断是否打开成功
	if (!ifs.is_open())
	{
		cout << "打开失败" << endl;
	}
	// 第一种方式
	char buf[1024];
	while (ifs >> buf)
	{
		cout << buf << endl;
	}

	// 第二种方式
	//char buf2[1024];
	//while (!ifs.eof()) // eof读到文件尾
	//{
	//	ifs.getline(buf2, sizeof(buf2));
	//	cout << buf2 << endl;
	//}
	//
	// 第三种方式 不推荐 按单个字符读取
	//char c;
	//while ((c = ifs.get()) != EOF) //EOF 文件尾
	//{
	//	cout << c;
	//}

	ifs.close();
}

int main(void)
{
	test();

	test01();
	
	return EXIT_SUCCESS;
}

输出:

姓名:小刘
性别:男
女朋友:暂无
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值