zoukankan      html  css  js  c++  java
  • C++ 核心编程

    C++ 核心编程

    引用

    **作用: **给变量起别名

    语法: 数据类型 &别名 = 原名

    引用是一种特殊的指针类型,引用一旦被定义就不能重新赋值,并且不能被设置为空值。

    int main() {
    	int a = 10;
    	int &b = a;
    
    
    	cout << "a = " << a << endl;
    	cout << "b = " << b << endl;
    
    	b = 100;
    	cout << "a = " << a << endl;
    	cout << "b = " << b << endl;
    
    结果:
    a = 10
    b = 10
    a = 100
    b = 100
    
    • 引用必须初始化
    • 引用在初始化后,不可以改变

    引用做函数参数

    作用:函数传参时,可以利用引用的技术让形参修饰实参

    优点:可以简化指针修改实参

    #include <iostream>
    using namespace std;
    
    
    //1. 值传递
    void mySwap01(int a, int b) {
    	int temp = a;
    	a = b;
    	b = temp;
    }
    
    //2. 地址传递
    void mySwap02(int* a, int* b) {
    	int temp = *a;
    	*a = *b;
    	*b = temp;
    }
    //3. 引用传递
    void mySwap03(int& a, int& b) {
    	
    	int temp = a;
    	a = b;
    	b = temp;
    }
    
    int main() {
    	int a = 10;
    	int b = 20;
    	//值传递
    	mySwap01(a, b);
    	cout << "a:" << a << " b:" << b << endl;
    
    	//地址传递
    	mySwap02(&a, &b);
    	cout << "a:" << a << " b:" << b << endl;
    	//引用传递
    	mySwap03(a, b);
    	cout << "a:" << a << " b:" << b << endl;
    	system("pause");
    
    
    
    }
    
    结果:
    a:10 b:20
    a:20 b:10
    a:20 b:10
    

    实参通常是通过值传递给函数的,这意味着形参接收的只是发送给它们的值的副本,它们存储在函数的本地内存中。对形参值进行的任何更改都不会影响原始实参的值。

    如果不想修改实参,就用值传递,如果想修改实参,就用地址传递或者引用传递

    引用做函数返回值

    作用:引用是可以作为函数的返回值存在的

    注意:不要返回局部变量引用

    用法:函数调用作为左值

    #include <iostream>
    using namespace std;
    
    //返回局部变量引用
    int& test01() {
    	int a = 10; //局部变量
    	return a;
    }
    
    
    //返回静态变量引用
    int& test02() {
    	static int a = 20;
    	return a;
    }
    
    int main(){
    	//不能返回局部变量的引用
    	int& ref = test01();
    	cout << ref << endl;  //结果:10
    	
    	cout << ref << endl;//结果:2037160328
    	
    	int& ref2 = test02();
    	cout << "ref2 = " << ref2 << endl;
    	cout << "ref2 = " << ref2 << endl;
    	//如果函数做左值,那么必须返回引用
    	test02() = 1000;
    
    	cout << "ref2 = " << ref2 << endl;
    	cout << "ref2 = " << ref2 << endl;
    
    return 0;
    }
    
    
    1. 来说下为什么不能返回局部变量的引用,来看代码int& ref2 = test01(); 这里ref1的引用时 test01方法,则应用的时改方法返回值内容。而在局部变量存放在四区的栈区中。在方法结束后就进行了释放。第一次钓鱼执行结果时正确的结果,时因为我编译器做了保留。而在一二次调用的时候内存进行了释放。
    2. 这里test2中为什么返回的是静态变量引用可以使用,不报错。是静态变量是存在在全局区中。
    3. 函数调用作为左值 实际上test方法中是返回a的引用,即相当于将a进行赋值1000

    引用的本质

    本质:引用的本质在c++内部实现是一个指针常量.

    常量引用

    作用:常量引用主要用来修饰形参,防止误操作

    void showValue(const int& v) {
    
    	cout << v << endl;
    
    }
    
    
    int main(){
    
    
    	const int& ref = 10;
    
    	cout << ref << endl;
    
    	int a = 10;
    
    	showValue(a);
    	showValue(ref);
    
    }
    

    函数提高

    函数默认参数

    在C++中,函数的形参列表中的形参是可以有默认值的。

    语法: 返回值类型 函数名 (参数= 默认值){}

    int func(int a = 10, int b = 20) {
    	return a + b;
    }
    
    
    
    
    
    
    int main(){
    
    	int a = func();
    	cout << a << endl;
        
    }
    

    函数占位参数

    C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置

    语法: 返回值类型 函数名 (数据类型){}

    void func(int a, int) {
    	cout << "this is func" << endl;
    }
    
    int main() {
    
    	func(10,10); //占位参数必须填补
    
    	system("pause");
    
    	return 0;
    }
    

    函数重载

    作用:函数名可以相同,提高复用性

    函数重载满足条件:

    • 同一个作用域下
    • 函数名称相同
    • 函数参数类型不同 或者 个数不同 或者 顺序不同
    //函数重载需要函数都在同一个作用域下
    void func()
    {
    	cout << "func 的调用!" << endl;
    }
    void func(int a)
    {
    	cout << "func (int a) 的调用!" << endl;
    }
    void func(double a)
    {
    	cout << "func (double a)的调用!" << endl;
    }
    void func(int a ,double b)
    {
    	cout << "func (int a ,double b) 的调用!" << endl;
    }
    void func(double a ,int b)
    {
    	cout << "func (double a ,int b)的调用!" << endl;
    }
    
    //函数返回值不可以作为函数重载条件
    //int func(double a, int b)
    //{
    //	cout << "func (double a ,int b)的调用!" << endl;
    //}
    
    
    int main() {
    
    	func();
    	func(10);
    	func(3.14);
    	func(10,3.14);
    	func(3.14 , 10);
    	
    	system("pause");
    
    	return 0;
    }
    

    函数重载注意事项

    • 引用作为重载条件
    • 函数重载碰到函数默认参数
    void func(int& a)
    {
    	cout << "func (int &a) 调用 " << endl;
    }
    
    void func(const int& a)
    {
    	cout << "func (const int &a) 调用 " << endl;
    }
    
    int main(){
    	int a = 10;
    	func(a); //调用无const
    	func(10);//调用有const
        
    }
    
    1. 来说明一下为什么a为什么func(a); 为什么调用的是没有const的方法。原因是因为a是一个变量。而func(10); ,10是常量。如果加载时上面的void func(int& a)方法,是不合法的。引用必须有一个合法的内存空间,而这里10是在常量区里面。

    类与对象

    封装

    语法: class 类名{ 访问权限: 属性 / 行为 };

    #include <iostream>
    
    using namespace std;
    const double PI = 3.14;
    class Circle
    {
    public:
    	int m_r;//半径
    
    	double calculateZC()
    	{
    		//2 * pi  * r
    		//获取圆的周长
    		return  2 * PI * m_r;
    	}
    };
    int main() {
    
    	Circle cl;
    	double bl = cl.calculateZC();
    
    		cout << bl << endl;
    
    	return 0;
    }
    

    struct和class区别

    在C++中 struct和class唯一的区别就在于 默认的访问权限不同

    区别:

    • struct 默认权限为公共
    • class 默认权限为私有

    成员属性设置为私有

    优点1:将所有成员属性设置为私有,可以自己控制读写权限

    优点2:对于写权限,我们可以检测数据的有效性

    class Person {
    public:
    
    	//姓名设置可读可写
    	void setName(string name) {
    		m_Name = name;
    	}
    	string getName()
    	{
    		return m_Name;
    	}
    
    
    	//获取年龄 
    	int getAge() {
    		return m_Age;
    	}
    	//设置年龄
    	void setAge(int age) {
    		if (age < 0 || age > 150) {
    			cout << "你个老妖精!" << endl;
    			return;
    		}
    		m_Age = age;
    	}
    
    	//情人设置为只写
    	void setLover(string lover) {
    		m_Lover = lover;
    	}
    
    private:
    	string m_Name; //可读可写  姓名
    	
    	int m_Age; //只读  年龄
    
    	string m_Lover; //只写  情人
    };
    
    
    int main() {
    
    	Person p;
    	//姓名设置
    	p.setName("张三");
    	cout << "姓名: " << p.getName() << endl;
    
    	//年龄设置
    	p.setAge(50);
    	cout << "年龄: " << p.getAge() << endl;
    
    	//情人设置
    	p.setLover("苍井");
    	//cout << "情人: " << p.m_Lover << endl;  //只写属性,不可以读取
    
    	system("pause");
    
    	return 0;
    }
    

    对象的初始化和清理

    c++利用了构造函数析构函数解决上述问题,这两个函数将会被编译器自动调用,完成对象初始化和清理工作。

    对象的初始化和清理工作是编译器强制要我们做的事情,因此如果我们不提供构造和析构,编译器会提供

    编译器提供的构造函数和析构函数是空实现。

    • 构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
    • 析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作。

    构造函数语法:类名(){}

    1. 构造函数,没有返回值也不写void
    2. 函数名称与类名相同
    3. 构造函数可以有参数,因此可以发生重载
    4. 程序在调用对象时候会自动调用构造,无须手动调用,而且只会调用一次

    析构函数语法: ~类名(){}

    1. 析构函数,没有返回值也不写void
    2. 函数名称与类名相同,在名称前加上符号 ~
    3. 析构函数不可以有参数,因此不可以发生重载
    4. 程序在对象销毁前会自动调用析构,无须手动调用,而且只会调用一次
    class Person
    {
    public:
    	//构造函数
    	Person()
    	{
    		cout << "Person的构造函数调用" << endl;
    	}
    	//析构函数
    	~Person()
    	{
    		cout << "Person的析构函数调用" << endl;
    	}
    
    };
    
    void test01()
    {
    	Person p;
    }
    
    int main() {
    
    	test01();
    
    	system("pause");
    
    	return 0;
    }
    

    析构函数对比构造函数

    相同点:与类名相同,没有返回值,如果用户不定义,系统也会自动生成一个空的析构函数。而一旦用户定义,则对象在销毁时自动调用。

    不同点:虽然他俩都为公开类型。构造可以重载,有多个兄弟,而析构却不能重载,但它可以是虚函数,一个类只能有一个析构函数。

    构造函数的分类及调用

    两种分类方式:

    ​ 按参数分为: 有参构造和无参构造

    ​ 按类型分为: 普通构造和拷贝构造

    三种调用方式:

    ​ 括号法

    ​ 显示法

    ​ 隐式转换法

    class Person {
    public:
    	//无参(默认)构造函数
    	Person() {
    		cout << "无参构造函数!" << endl;
    	}
    	//有参构造函数
    	Person(int a) {
    		age = a;
    		cout << "有参构造函数!" << endl;
    	}
    	//拷贝构造函数
    	Person(const Person& p) {
    		age = p.age;
    		cout << "拷贝构造函数!" << endl;
    	}
    	//析构函数
    	~Person() {
    		cout << "析构函数!" << endl;
    	}
    public:
    	int age;
    };
    
    //2、构造函数的调用
    //调用无参构造函数
    void test01() {
    	Person p; //调用无参构造函数
    }
    
    //调用有参的构造函数
    void test02() {
    
    	//2.1  括号法,常用
    	Person p1(10);
    	//注意1:调用无参构造函数不能加括号,如果加了编译器认为这是一个函数声明
    	//Person p2();
    
    	//2.2 显式法
    	Person p2 = Person(10); 
    	Person p3 = Person(p2);
    	//Person(10)单独写就是匿名对象  当前行结束之后,马上析构
    
    	//2.3 隐式转换法
    	Person p4 = 10; // Person p4 = Person(10); 
    	Person p5 = p4; // Person p5 = Person(p4); 
    
    	//注意2:不能利用 拷贝构造函数 初始化匿名对象 编译器认为是对象声明
    	//Person p5(p4);
    }
    
    int main() {
    
    	test01();
    	return 0;
    }
    

    拷贝构造函数调用时机

    C++中拷贝构造函数调用时机通常有三种情况

    • 使用一个已经创建完毕的对象来初始化一个新对象
    • 值传递的方式给函数参数传值
    • 以值方式返回局部对象
    class Person {
    public:
    	Person() {
    		cout << "无参构造函数!" << endl;
    		mAge = 0;
    	}
    	Person(int age) {
    		cout << "有参构造函数!" << endl;
    		mAge = age;
    	}
    	Person(const Person& p) {
    		cout << "拷贝构造函数!" << endl;
    		mAge = p.mAge;
    	}
    	//析构函数在释放内存之前调用
    	~Person() {
    		cout << "析构函数!" << endl;
    	}
    public:
    	int mAge;
    };
    
    //1. 使用一个已经创建完毕的对象来初始化一个新对象
    void test01() {
    
    	Person man(100); //p对象已经创建完毕
    	Person newman(man); //调用拷贝构造函数
    	Person newman2 = man; //拷贝构造
    
    	//Person newman3;
    	//newman3 = man; //不是调用拷贝构造函数,赋值操作
    }
    
    //2. 值传递的方式给函数参数传值
    //相当于Person p1 = p;
    void doWork(Person p1) {}
    void test02() {
    	Person p; //无参构造函数
    	doWork(p);
    }
    
    //3. 以值方式返回局部对象
    Person doWork2()
    {
    	Person p1;
    	cout << (int *)&p1 << endl;
    	return p1;
    }
    
    void test03()
    {
    	Person p = doWork2();
    	cout << (int *)&p << endl;
    }
    
    
    int main() {
    
    	//test01();
    	//test02();
    	test03();
    
    	system("pause");
    
    	return 0;
    }
    

    初始化列表

    class Person {
    public:
    
    	//初始化列表方式初始化
    	Person(int a, int b, int c) :m_A(a), m_B(b), m_C(c) {}
    	void PrintPerson() {
    		cout << "mA:" << m_A << endl;
    		cout << "mB:" << m_B << endl;
    		cout << "mC:" << m_C << endl;
    	}
    private:
    	int m_A;
    	int m_B;
    	int m_C;
    };
    
    int main() {
    
    	Person p(1, 2, 3);
    	p.PrintPerson();
    
    
    	system("pause");
    
    	return 0;
    }
    

    类对象作为类成员

    静态成员

    静态成员就是在成员变量和成员函数前加上关键字static,称为静态成员

    class Person
    {
    	
    public:
    
    	static int m_A; 
        private:
    	static int m_B; //静态成员变量也是有访问权限的
    };
        
        
    

    静态成员函数

    class Person
    {
    
    public:
    	static void func()
    	{
    		cout << "func调用" << endl;
    		m_A = 100;
            
        }
    

    C++对象模型和this指针

    this指针概念

    this指针指向被调用的成员函数所属的对象

    this指针是隐含每一个非静态成员函数内的一种指针

    this指针不需要定义,直接使用即可

    this指针的用途:

    • 当形参和成员变量同名时,可用this指针来区分
    • 在类的非静态成员函数中返回对象本身,可使用return *this。
    class Person
    {
    public:
    
    	int age;
    
    	Person(int age) {
    		this->age = age;
    	
    	}
    	Person& PersonAddperson(Person person) {
    
    		this->age += person.age;
    		return *this;
    	}
    
    };
    
    void test() {
    	Person p1(10);
    	cout << "p1.age = " << p1.age << endl;
    	Person p2(10);
    
    	p2.PersonAddperson(p1).PersonAddperson(p1).PersonAddperson(p1);
    }
    
    int main() {
    
    	test();
    
    	system("pause");
    
    	return 0;
    }
    

    空指针访问成员函数

    C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针

    如果用到this指针,需要加以判断保证代码的健壮性。

    class Person
    {
    public:
    
    	int age;
    
    	void ShowClassName() {
    		cout << "我是Person类!" << endl;
    	}
    
    	void ShowPerson() {     //但是如果成员函数中用到了this指针,就不可以了
    		if (this == NULL) {
    			return;
    		}
    		cout << mAge << endl;
    	}
    
    public:
    	int mAge;
    };
    
    void test01()
    {
    	Person* p = NULL;
    	p->ShowClassName(); //空指针,可以调用成员函数
    	//p->ShowPerson();  //错误,但是如果成员函数中用到了this指针,就不可以了
    }
    
    int main() {
    
    	test01();
    	system("pause");
    
    	return 0;
    }
    
    1. 这里空指针去调用p->ShowPerson();失败,其原因是因为ShowPerson()方法中使用了mAge而这个变量为this->mAge,该位置是空指针,this为空。所以这里报错了。

    const修饰成员函数

    常函数:

    • 成员函数后加const后我们称为这个函数为常函数
    • 常函数内不可以修改成员属性
    • 成员属性声明时加关键字mutable后,在常函数中依然可以修改
    void showPerson() const{
        
    }
    

    常对象:

    • 声明对象前加const称该对象为常对象
    • 常对象只能调用常函数
    const Person p;
    p.a = 100;//报错
    

    友元

    友元的目的就是让一个函数或者类 访问另一个类中私有成员

    友元的三种实现

    • 全局函数做友元
    • 类做友元
    • 成员函数做友元

    全局函数做友元

    class Building
    {
    
    	friend void goodGay(Building* building);//告诉编译器 goodGay全局函数 ,可以访问类中的私有内容
    public:
    	Building() {
    		this->m_SittingRoom = "客厅";
    		this->m_BedRoom = "卧室";
    	}
    	
    public:
    	string m_SittingRoom; //客厅
    
    private:
    	string m_BedRoom; //卧室
    
    
    };
    void goodGay(Building* building)
    {
    	cout << "好基友正在访问: " << building->m_SittingRoom << endl;
    	cout << "好基友正在访问: " << building->m_BedRoom << endl;
    }
    void test01()
    {
    	Building b;
    	goodGay(&b);
    }
    
    int main() {
    	test01();
    	system("pause");
    	return 0;
    
    }
    

    实际上就是在类的头部声明一个friend修饰的函数,也就是编写一个全局函数。然后使用该类去进行访问类中的private修饰的变量等。

    类做友元

    #include <iostream>
    using namespace std;
    
    
    class Building;
    class goodGay
    {
    public:
    	goodGay();
    	void visit();
    
    private:
    	Building *building;
    
    };
    
    class Building
    {
    	friend class goodGay;
    public:
    	Building();
    
    public:
    	string m_SittingRoom; //客厅
    
    
    private:
    	string m_BedRoom;//卧室
    };
    
    Building::Building() //类外实现
    {
    	this->m_SittingRoom = "客厅";
    	this->m_BedRoom = "卧室";
    
    }
    
    goodGay::goodGay()
    {
    	building = new Building;
    }
    
    void goodGay::visit() {
    	cout << "好基友正在访问" << building->m_SittingRoom << endl;
    	cout << "好基友正在访问" << building->m_BedRoom << endl;
    }
    
    
    void test01()
    {
    	goodGay gg;
    	gg.visit();
    
    }
    
    
    int main() {
    	
    	system("pause");
    	return 0;
    
    }
    

    这里看起来比较绕,当时这里逻辑捋清楚就好了。首先是创建一个goodGay实例,创建实例的构造方法中会去new一个Building。Building类构造方法则对m_SittingRoom,m_BedRoom进行赋值。

    下面visit方法则打印两个变量。这里之所以可以访问类的私有变量是因为friend class goodGay;这行代码。声明了该类有权限进行访问。

    成员函数做友元

    class Building
    {
    	//告诉编译器  goodGay类中的visit成员函数 是Building好朋友,可以访问私有内容
    	friend void goodGay::visit();
        
    }
    

    与上同理。

    继承

    继承的语法:class 子类 : 继承方式 父类

    继承方式一共有三种:

    • 公共继承
    • 保护继承
    • 私有继承

    继承中 先调用父类构造函数,再调用子类构造函数,析构顺序与构造相反

    继承同名成员处理方式

    int main()
    {
    	Son s;
    
    	cout << "Son下的m_A = " << s.m_A << endl;
    	cout << "Base下的m_A = " << s.Base::m_A << endl;
    
    	s.func();
    	s.Base::func();
    	s.Base::func(10);
    
    }
    

    总结:

    1. 子类对象可以直接访问到子类中同名成员
    2. 子类对象加作用域可以访问到父类同名成员
    3. 当子类与父类拥有同名的成员函数,子类会隐藏父类中同名成员函数,加作用域可以访问到父类中同名函数

    继承同名静态成员处理方式

    • 访问子类同名成员 直接访问即可
    • 访问父类同名成员 需要加作用域

    多继承语法

    C++允许一个类继承多个类

    语法: class 子类 :继承方式 父类1 , 继承方式 父类2...

    多继承可能会引发父类中有同名成员出现,需要加作用域区分

    多态

    多态满足条件:

    1、有继承关系
    2、子类重写父类中的虚函数
    多态使用:
    父类指针或引用指向子类对象

    class Animal
    {
    
    
    public:
    
    	virtual void speak() {
    		cout << "动物在说话" << endl;
    	
    	}
    private:
    
    };
    
    
    class Cat :public Animal
    {
    public:
    	void speak()
    	{
    		cout << "小猫在说话" << endl;
    	}
    };
    
    
    
    class Dog :public Animal
    {
    public:
    
    	void speak()
    	{
    		cout << "小狗在说话" << endl;
    	}
    
    };
    void DoSpeak(Animal &an)
    {
    	an.speak();
    }
    
    int main() {
    	Cat cat;
    	DoSpeak(cat);
    
    }
    
    

    这里是编写了一个方法传递引用Animal,并且调用speak

    纯虚函数和抽象类

    在多态中,通常父类中虚函数的实现是毫无意义的,主要都是调用子类重写的内容。

    语法:virtual 返回值类型 函数名 (参数列表)= 0 ;

    当类中有了纯虚函数,这个类也称为抽象类。

    抽象类特点

    • 无法实例化对象
    • 子类必须重写抽象类中的纯虚函数,否则也属于抽象类
    class Base
    {
    public:
    	virtual void func() = 0;
    
    
    };
    
    
    class Son :public Base
    {
    	 void func() {
    		cout << "Son func调用" << endl;
    	}
    
    };
    
    
    
    void dofunc(Base &base) {
    	base.func();
    
    }
    
    int main() {
    	Base * base = NULL;
    	base = new Son; //base = new Base; // 错误,抽象类无法实例化对象
    	base->func();
    
    }
    

    虚析构和纯虚析构

    多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类的析构代码

    解决方式:将父类中的析构函数改为虚析构或者纯虚析构

    虚析构和纯虚析构共性:

    • 可以解决父类指针释放子类对象
    • 都需要有具体的函数实现

    虚析构和纯虚析构区别:

    • 如果是纯虚析构,该类属于抽象类,无法实例化对象

    虚析构语法:

    virtual ~类名(){}

    纯虚析构语法:

    virtual ~类名() = 0;

    类名::~类名(){}

    文本文件操作

    写文件

    写文件步骤如下:

    1. 包含头文件

      #include <fstream>

    2. 创建流对象

      ofstream ofs;

    3. 打开文件

      ofs.open("文件路径",打开方式);

    4. 写数据

      ofs << "写入的数据";

    5. 关闭文件

      ofs.close();

    打开方式 解释
    ios::in 为读文件而打开文件
    ios::out 为写文件而打开文件
    ios::ate 初始位置:文件尾
    ios::app 追加方式写文件
    ios::trunc 如果文件存在先删除,再创建
    ios::binary 二进制方式
    int main() {
    	ofstream ofs;
    	ofs.open("test.txt", ios::out);
    	ofs << "姓名:张三" << endl;
    	ofs << "性别:男" << endl;
    	ofs << "年龄:18" << endl;
    	ofs.close();
    	return 0;
    
    }
    

    总结:

    • 文件操作必须包含头文件 fstream
    • 读文件可以利用 ofstream ,或者fstream类
    • 打开文件时候需要指定操作文件的路径,以及打开方式
    • 利用<<可以向文件中写数据
    • 操作完毕,要关闭文件

    读文件

    读文件步骤如下:

    1. 包含头文件

      #include <fstream>

    2. 创建流对象

      ifstream ifs;

    3. 打开文件并判断文件是否打开成功

      ifs.open("文件路径",打开方式);

    4. 读数据

      四种方式读取

    5. 关闭文件

      ifs.close();

    #include <fstream>
    
    
    
    void test() {
    
    	ifstream ifs;
    
    	ifs.open("test.txt", ios::in);
    
    	if (!ifs.is_open()) {
    		cout << "文件打开失败" << endl;
    		return;
    	}
    
    
    	char c;
    	while ((c = ifs.get()) != EOF)
    	{
    		cout << c;
    	}
    
    	ifs.close();
    }
    
    int main() {
    	test();
    	return 0;
    
    }
    

    总结:

    • 读文件可以利用 ifstream ,或者fstream类
    • 利用is_open函数可以判断文件是否打开成功
    • close 关闭文件

    二进制文本操作

    写文件

    二进制方式写文件主要利用流对象调用成员函数write

    函数原型 :ostream& write(const char * buffer,int len);

    参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数

    void test() {
    
    	ofstream ofs("person.txt", ios::out | ios::binary);
    
    
    	Person p = { "张三"  , 18 };
    
    	ofs.write((const char*)&p, sizeof(p));
        
    }
    
    int main(){
        
        test()
        
    }
    

    读文件

    二进制方式读文件主要利用流对象调用成员函数read

    函数原型:istream& read(char *buffer,int len);

    参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数

    class Person
    {
    public:
    	char m_Name[64];
    	int m_Age;
    };
    
    void test() {
    
    	ifstream ifs("person.txt", ios::in | ios::binary);
    	if (!ifs.is_open())
    	{
    		cout << "文件打开失败" << endl;
    	}
    	Person p;
    	ifs.read((char*)&p, sizeof(p));
    
    	cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
        
    }
    
    int main() {
    	
    	test();
    
    	return 0;
    	
    
    }
    
  • 相关阅读:
    shell 基本系统命令,关机重启,查看版本,查手册,日期,磁盘,历史命令
    shell 命令 文件查看ls,复制cp,移动mv,查看文件内容cat more less,查看文件信息 file
    luoguP1850 换教室
    bzoj2091: [Poi2010]The Minima Game DP
    luoguP1281 书的复制 DP,贪心
    loj6068. 「2017 山东一轮集训 Day4」棋盘 二分图,网络流
    bzoj1133: [POI2009]Kon
    luogu3426 [POI2005]SZA-Template 后缀树
    loj#2483. 「CEOI2017」Building Bridges 斜率优化 cdq分治
    loj2353. 「NOI2007」 货币兑换
  • 原文地址:https://www.cnblogs.com/nice0e3/p/15137915.html
Copyright © 2011-2022 走看看