c++——文件操作2-17和一周学习目录

C++

初级C++

1)C++和c区别:
C++面向对象,C面向过程。
C++函数例:hello.cpp
#include//头文件
using namespace std;//命名空间
int main()
{
    cout <<  “Hello World” << endl;//输出
    return 0;
}
全局变量和局部变量

作用域运算符 : :
const限定符

复合类型

数组类
字符串char
string类(字符串)
这三个和c相同

结构体 class(类似于struct)
public:共有
private:私有的
protect:保护的

内存管理

指针:
创建空间new(malloc)
释放空间delete(free)

引用:&
没有自身空间,只是引用。引用时加const

函数

c++内联函数
引用变量
默认参数
函数重载:
函数模板:模糊定义 define和typedef

高级C++

类继承:

单继承class Student:public Teacher

多继承class C:public A : public B

访问控制

虚继承:是指一个指定的基类,在继承体系结构中,将其成员数据实例共享给也从这个基类型直接或间接派生的其它类

虚函数:virtual

虚表:每个有虚函数的类或者虚继承的子类,编译器都会为它生成一个虚拟函数表(简称:虚表),表中的每一个元素
都指向一个虚函数的地址

虚表指针:如果一个类含有虚表,则该类的所有对象都会含有一个虚表指针,并且该虚表指针指向同一个虚表

多态:接口的多种不同的实现方式即为多态

纯虚函数:在基类中不能对虚函数给出有意义的实现

成员容器

容器:vector,有序存入,下标访问

链表:list,和C中链表相同

map:有序存入数据

迭代器:iterator;用来遍历标准模板库容器中的部分或全部元素

类和对象

类的构造函数:创建对象时初始化对象

析构函数:~Student(){},在退出时自动回收空间

this指针

类作用域

const/static:静态变量和静态全局变量

友元函数:friend,在public中定义,可访问部分或全部private数据

符号重载:operator,

friend ostream& operator<< (ostream &out
                                , const Student &stu);
ostream& operator<< (ostream &out, const Student &stu)
{
    out << stu.m_strName << ' ' << stu.m_fScore;
    return out;
}

类的动态内存分配

拷贝构造函数:引用,会加上const限制,拷贝构造函数要调用基类的拷贝构造函数和成员函数

浅拷贝:只有类成员

深拷贝:还有地址

文件操作

读: ifstream 默认读的方式打开;ifs >> stu;;ios_base::in;ios_base::binary二进制写

写:ofstream默认写、截断(覆盖)、文本的方式打开文件,ofs.open打开;ios_base::app追加

读和写:fstream:cout << file.tellp() << endl;显示读写位置;
file.seekg(2*sizeof(Student),ios_base::cur);偏移两个Student距离

关闭:ofs.close()

#include 
#include 
#include "student.h"

using namespace std;
#if 1
int main(void)
{
#if 0
    //默认读写的方式打开文件
    fstream file;
    file.open("stu.info");
#endif

    //默认读写的方式打开文件
    fstream file("stu.info");
    if (file.is_open())
    {
        cout << "open file ok \n";
#if 0
        cout << file.tellp() << endl;
//      file.seekp(sizeof(Student), ios_base::beg);
//      ios_base::beg   从开头开始 begin
        file.seekg(sizeof(Student), ios_base::beg);
//        cout << file.tellp() << endl;
        cout << file.tellg() << endl;

        Student stu;
        file.read((char *)&stu, sizeof(stu));
        cout << stu << endl;

//        file.seekp(2*sizeof(Student), ios_base::cur);
        file.seekg(2*sizeof(Student), ios_base::cur);
        file.read((char *)&stu, sizeof(stu));//读
        cout << stu << endl;
#endif
        Student newStu("laojiu", 99, 79);
        file.seekp(0, ios_base::end);
        file.write((char *)&newStu, sizeof(newStu));//写入
        cout << file.tellp() << endl;//输入读写流指针位置
        cout << file.tellg() << endl;//输出流指针读写位置

        int ilen = sizeof(Student);
        //从结尾向前移动-ilen个距离读
        file.seekp(-ilen, ios_base::end);
        cout << file.tellg() << endl;

        Student stu;
        file.read((char *)&stu, sizeof(stu));
        cout << stu << endl;
        cout << file.tellg() << endl;

        cout << "Hello World" << endl;

        file.close();
    }
    else
    {
        cout << "open file failed\n";
    }


    return 0;
}
#endif

#if 0
int main(void)
{
    //读入,二进制方式
    ifstream ifs("test.dat"
                 , ios_base::in|ios_base::binary);

    if (ifs.is_open())
    {
        cout << "open file ok\n";
        Student stu;
        ifs.read((char *)&stu, sizeof(stu));
        cout << stu << endl;
        ifs.close();
    }
    else
    {
        cout << "open file failed\n";
    }
    return 0;
}
#endif

#if 0
int main(void)
{
    //    ifstream ifs;
    //默认读的方式打开文件
    //    ifs.open("test.dat");

    //默认读的方式打开文件
    ifstream ifs("test.dat");
    if (ifs.is_open())
    {
        cout << "open file ok\n";
        string str;
        int iData;
        float fData;
        Student stu;

        ifs >> str >> iData >> fData;
        ifs >> stu;
        cout << str << ' ' << iData
             << ' ' << fData << endl;
        cout << stu << endl;
    }
    else
    {
        cout << "open file failed\n";
    }
}
#endif

#if 0
int main(void)
{
#if 0
    ofstream ofs;
    //如果文件不存在则会自动创建
    //默认写,截断,文本的方式打开文件
    ofs.open("test.dat");
#endif
#if 0
    //在创建对象的时候指定文件,该文件会在构造函数中被打开
    //默认写,截断,文本的方式打开文件
    ofstream ofs("test.dat");
#endif
    ofstream ofs;

    //写,追加,文本的方式打开文件
    ofs.open("test.dat", ios_base::out|ios_base::app);
    if (ofs.is_open())
    {
        cout << "open file ok" << endl;
        ofs << "HelloWorld " << ' ' << 123
            << ' ' << 3.45 << endl;

        Student stu("sasaxas", 129, 78);
        ofs << stu << endl;

        ofs.close();
    }
    else
    {
        cout << "open file failed " << endl;
    }
    return 0;
}
#endif

你可能感兴趣的:(c++——文件操作2-17和一周学习目录)