C++二进制文件的读与写

作者 : admin 本文共3031个字,预计阅读时间需要8分钟 发布时间: 2024-06-17 共1人阅读

1. 二进制文件的读与写ofstream fout; ifstream fin;

——学习把结构体类型写到二进制文件中。
代码实现:

/*
_4.cpp 学习总结 视频学习C++_Primer_Plus_17_6
1. 二进制文件的读与写
2. 写操作:fout类比cout操作,cout能操作的fout也适用
3. 易错点1:读写操作时,读in,写out,不可写错
    易错点2:写操作时,添加要求时,貌似或(|)与或之间的顺序也要注意
4. fout.write((char *) &p1, sizeof p1);
    解释两个参数
    参数1:地址。&p1表示地址,(char *)强转,固定,
    参数2:字节大小。
    读函数同理
    类知识中对这个两个函数有详细讲解
*/
#include 
#include 
#include  //输出格式用

//内联函数吃没用的字符
inline void eatline() { while(std::cin.get() != '
') continue; }

struct Planet
{
    char name[20];
    double papolation;
    double g;
};//结构体星球

const char* filename = "plants.dat";

int main()
{
    using namespace std;

    //写进二进制文件
    ofstream fout(filename, ios_base::out | ios_base::app | ios_base::binary);

    Planet p1;   
    cout << "以空白行结束输入:" <> p1.papolation;
        cin >> p1.g;
        eatline();//吃没用的字符
        
        //写操作
        
        fout.write((char *) &p1, sizeof p1);
        
        cout << "以空白行结束输入:" << endl;
        cin.get(p1.name, 20);
    }
    fout.close();

    //读二进制文件
    ifstream fin;
    fin.open(filename, ios_base::in | ios_base::binary);
    if(!fin.is_open())
    {
        cout << "打不开此文件:" << filename << endl;
        exit(EXIT_FAILURE);
    }

    cout << fixed;//不用科学计数法显示
    cout.precision(2);//精度保留两位小数
    cout.setf(ios_base::showpoint);//显示小数点,不写(输入135.0,显示135)
    cout << "读文件并显示:" << endl;
    while(fin.read((char *) &p1, sizeof p1))
    {
        cout << "Name: " << p1.name << endl;
        cout << "Population: " << p1.papolation << endl;
        cout << "G: " << p1.g << endl;
    }

    fin.close();
    return 0;
}

2. 在二进制文件的任意位置进行存储或者修改 fstream finout;

——修改二进制文件中的结构体内容。
代码实现:

/*
_5.cpp 学习总结 视频学习C++_Primer_Plus_17_7
1.  修改二进制文件中的结构体内容
	planet.dat二进制文件已存在(内容为4个Planet)
    该示例将完成以下工作:
    a. 询问要修改哪条记录
    b. 显示未修改的记录 
    c. 显示已修改的记录

2. seekg():将输入指针移至指定的文件位置
    seekp():将输出指针移到指定文件位置
    原型1:seekg(streampos) streampos place = rec * sizeof p1;
    原型2:seek(streamoff, ios_base::seekdir) 未应用
3. fstream finout; 读写都可以
*/
#include 
#include 
#include  //输出格式用

//内联函数吃没用的字符
inline void eatline() { while(std::cin.get() != '
') continue; }

struct Planet
{
    char name[20];
    double papolation;
    double g;
};//结构体星球

const char* filename = "plants.dat";

int main()
{
    using namespace std;

    Planet p1;
    fstream finout;

    //打开二进制文件,可能读也可能写,planet.dat二进制文件已存在(内容为4个Planet)
    finout.open("planets.dat", ios_base::in | ios_base::out | ios_base::binary);
    if(!finout.is_open())
    {
        cout << "打不来此文件:planets.dat" << endl;
        exit(EXIT_FAILURE); 
    }

    //修改第 rec 个结构体的内容,rec(0,1,2,3...),place即是第rec的首地址
    cout <> rec; eatline();
    streampos place = rec * sizeof p1;
    
    //文件输出指针重新定位
    finout.seekg(place);
    //把该位置的内容存到p1内
    finout.read((char *)&p1, sizeof p1);
    
    //显示该位置本来内容
    cout << "显示该位置本来内容: " << endl;
    cout << "Name: " << p1.name << endl;
    cout << "Poluation: " << p1.papolation << endl;
    cout << "G: " << p1.g << endl;

    //输入要修改的的内容:
    cout << "输入要修改的的内容:" << endl;
    cout <> p1.name;
    cout <> p1.papolation;
    cout <> p1.g;
    eatline();
    //文件输入指针重新定位,然后写入
    finout.seekp(place);
    finout.write((char *)&p1, sizeof p1);

    
    //再次显示该位置内容:
    cout << "再次显示该位置内容:" << endl;
    finout.seekg(place);
    finout.read((char *)&p1, sizeof p1);
    cout << "Name: " << p1.name << endl;
    cout << "Poluation: " << p1.papolation << endl;
    cout << "G: " << p1.g << endl;

    return 0;
}

3. 知识点。

常量含义
ios_base::in打开文件,以便读取
ios_base::out打开文件
ios_base::out打开文件,以便写入
ios_base::app追加到文件末尾
ios_base::trunc如果文件存在,清空文件内容
ios_base::binary二进制文件
C++模式C模式含义
ios_base::in“r”打开文件,以便读取
ios_base::out“w”等价于ios_base::out | ios_base::trunc
ios_base::out | ios_base::trunc“w”打开以写入,如果文件存在,先清空文件
ios_base::out | ios_base::app“a”打开以写入,只追加
ios_base::in | ios_base::out“r+”打开以读写,在文件允许的位置写入
ios_base::in | ios_base::out | ios_base::trunc“w+”打开以读写,如果文件按存在,清空文件
本站无任何商业行为
个人在线分享 » C++二进制文件的读与写
E-->