C++进阶(14)类型转换、IO流

一、类型转换

当两个类型不是严格的相同的时候,就会有类型转换的问题。

C语言

隐式类型转换和强制类型转换。

隐式类型转换

适用于类型相似的内置类型(整型、浮点数、字符型)之间进行类型转化。比如:整型和字符型之间,整型和浮点数之间。

int a = 10;
double b = a; // 整型和浮点数之间
char c = a; // 整型和字符型之间

强制类型转换

适用于整型和指针之间,指针和指针之间。

int a = 10;
int* pa = &a;
int b = (int)pa; // 整型和指针之间
double* pb = (double*)b; // 指针和指针之间

C++

类型转换的情况

C++不仅仅兼容C的类型转换,还有一些自定义类型之间的转换。

内置类型 -> 自定义类型

自定义类型 -> 内置类型

自定义类型 -> 自定义类型

// 内置类型转换成自定义类型
// 通过构造函数实现的
class A
{
private:
	int _a;
	int _b;
public:
	A(int a, int b)
		:_a(a)
		, _b(b)
	{}
};
A a(1, 3);

// 自定义类型转换成内置类型
// 通过get函数,将A类型转换成int
class A
{
private:
	int _a;
	int _b;
public:
	friend int get(A& a);
	A(int a, int b)
		:_a(a)
		, _b(b)
	{}
};
int get(A& a)
{
	return a._a;
}

// 自定义类型转换成自定义类型
// 将子类赋值给父类,也可以通过构造函数实现,方法不唯一
class A
{
private:
	int _a;
	int _b;
public:
	A(int a = 1, int b = 1)
		:_a(a)
		, _b(b)
	{}
};
class B:public A
{
private:
	int _c;
public:
	B(int c = 1)
		:_c(c)
	{}
};
B b;
A a = b;

类型转换的函数(4个)

// 隐式类型转换:static_cast<T>
int a = 10;
double b = 3.4;
a = static_cast<int>(b);
// 强制类型转换:reinterpret<T>
int a = 10;
int* pa = &a;
int b = reinterpret_cast<int>(pa);
// const类型转换:const_cast<T> --- 用于删除变量的const属性
const int x = 10;
int* px = const_cast<int*>(&x);
*px = 4;
std::cout << *px << std::endl;
// 向下转换:dynamic_cast<T>(父类对象指针/引用 -> 子类指针/引用)
class A
{
public:
	virtual void print()
	{
		std::cout << "A: print()" << std::endl;
	}
};
class B : public A
{
public:
	virtual void print()
	{
		std::cout << "B: print()" << std::endl;
	}
};
A* a = new B;
B* b = dynamic_cast<B*>(a);
b->print();

二、IO流

1、缓冲区

输入输出是有缓冲区的概念的,根据Linux中学的一切皆文件,大概的也能明白(因为文件里面有缓冲区)。

// 根据下面的代码可以深刻的体会出缓冲区的概念。
// 一次性输入10x
// 下面会直接输出:
// 10
// x
// 我们只输入的了一次,为什么会直接都输出出来呢?
// 答:当我们输入10x的时候,第一次因为变量a是int类型,他只读到了10,然后就直接退出了,而x还在缓冲区中。当缓冲区不空的时候,我们是没有办法继续往里面进行读数据的,需要将其进行接收。然后ch正好是字符类型,接收了x,所以最后会直接打印出来,而不需要我们输入两次。

// 所以为了解决某种用户输入不正确的数据时,需要我们进行判断,然后对缓冲区进行清空,以便不影响下一次输入。
#include <iostream>
int main()
{
	int a = 0;
	std::cin >> a;
	std::cout << a << std::endl;

	char ch = 0;
	std::cin >> ch;
	std::cout << ch << std::endl;
	return 0;
}

2、提高输入输出效率

// 第一种方式:--- 用C语言的输入输出
//printf
//scanf

// 第二种方式 --- C++
// 在main函数中加入这三句语句,一个main函数只需要写一次
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);

3、文件操作

进行文件操作的时候只需要包含fstream即可。

下面是文件头文件的结构图。

image-20240820195456482

文件权限

  1. ios::in:以输入(读取)模式打开文件。
  2. ios::out:以输出(写入)模式打开文件。
  3. ios::app:以追加模式打开文件,写入的数据会追加到文件末尾。
  4. ios::ate:打开文件时,立即将文件指针移动到文件末尾。
  5. ios::trunc:如果文件已存在,打开时清空文件内容。
  6. ios::binary:以二进制模式打开文件,而不是默认的文本模式。

写操作 — ofstream

文本方式写入
#include <iostream>
#include <fstream>

int main()
{
	// 以文本方式打开文件(如果默认路径下没有文件,则新建文件)、
    // 默认的权限是ios::out
    // std::ofstream fout("test.txt", std::ios::out); --- 以写的方式打开文件
    // std::ofstream fout("test.txt", std::ios::out | std::ios::app); --- 以追加模式的写的方式打开文件
    // ...
	std::ofstream fout("test.txt");
	// 文本输入
	fout << "12434\n";
	fout << "abcdefg";
	fout << "hijk";
	// 关闭文件
	fout.close(); 
	return 0;
}
二进制方式写入
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

int main()
{
	std::vector<int> v = { 1, 2, 3, 4 };
	// 打开文件,以写的方式
	std::ofstream fout("test.txt", std::ios::out | std::ios::binary);
	// 写入文件
    // ostream& write (const char* s, streamsize n);
	for (auto& e : v)
	{
		fout.write(reinterpret_cast<char*>(&e), sizeof(e));
	}
	// 关闭文件
	fout.close();
	return 0;
}

读操作 — ifstream

文本方式读取
#include <iostream>
#include <string>
#include <fstream>

int main()
{
	// 打开文件,以读的方式
	std::ifstream fin("test.txt");
	// 读取文件
	std::string tmp;
	while (fin >> tmp)
	{
		std::cout << tmp << std::endl;
	}
	// 关闭文件
	fin.close();
	return 0;
}
二进制方式读取
// 以什么方式写,就以什么方式读
// 也要注意数据的格式,如果是整型就整形方式读,自定义类型就自定义类型方式读
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

int main()
{
	// 打开文件,以读的方式
	std::ifstream fin("test.txt", std::ios::in | std::ios::binary);
	// 读取文件
	char ch;
	while (fin.read(&ch, sizeof(ch)))
	{
		std::cout << ch;
	}
    if (fin.good())
    {
        std::cout << "读取成功" << std::endl;
    }
	// 关闭文件
	fin.close();
	return 0;
}

image-20240820220212954

其他操作

判断文件是否成功打开?
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

int main()
{
	// 打开文件,以读的方式
	std::ifstream fin("test.txt", std::ios::in | std::ios::binary);
	// 判断是否成功打开文件
	if (!fin)
	{
		std::cout << "无法打开文件!" << std::endl;
		return 1;
	}
	// 关闭文件
	fin.close();
	return 0;
}

谢谢大家!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

仍有未知等待探索

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

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

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

打赏作者

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

抵扣说明:

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

余额充值