二进制文件读写



1.二进制写入,ios::binary

#include<iostream>

#include <fstream>

#include <string>

 

using namespace std;

 

struct MyStruct

{

    char *p = "北京是帝都";

    int num = 20;

    double db = 10.98;

    char ch = 'a';

};

 

void main1()

{

    ofstream fout("E:\\wen.txt", ios::out);

    ifstream fin("E:\\wen.txt");

    //下面的大小时24

    std::cout << sizeof(MyStruct) << std::endl;

    MyStruct my1;

    //向文件中写入内容

    fout << my1.p << " " << my1.num << " " << my1.db << " " << my1.ch << "\n";

    fout.close();

    char str[100] = { 0 };

    //提取文件内容

    fin.getline(str, 100, 0);

    std::cout << str << std::endl;

    fin.close();

    cin.get();

    //运行结果是:

    //24

    //北京是帝都 20 10.98 a

    //同时在E盘生成了wen.txt

}

 

void main()

{

    MyStruct my1;

    my1.p = "chuheridangwu";

    //写入文件,按照二进制的方式写入,要加上:ios::binary

    ofstream fout("C:\\bin.bin", ios::binary);

    fout.write((char *)&my1, sizeof(my1));

    //第一个参数是要写入文件的内存的首地址

    //第二个参数是长度

    fout.close();

    ifstream fin("C:\\bin.bin", ios::binary);

    MyStruct newmy1;

    fin.read((char *)&newmy1, sizeof(newmy1));

    //保存文件读取到内存,内存首地址

    //长度

    std::cout << newmy1.p << std::endl;

    fin.close();

 

    std::cin.get();

//这里的运行结果是:chuheridangwu

}

2.字节的二进制

#include<iostream>

#include <fstream>

#include <string>

 

using namespace std;

 

void main()

{

    ifstream fin("C:\\write.exe", ios::binary);

    ofstream fout("C:\\newwrite.exe", ios::binary);

    if (!fin || !fout)

    {

        std::cout << "文件打开失败";

        return;

    }

    std::cout << "文件拷贝开始\n";

    char ch = 0;

    while (fout && fin.get(ch))//引用的方式读取到一个字符

    {

        fout.put(ch);//写入一个字节

 

    }

    fin.close();

    fout.close();

 

    std::cout << "文件拷贝完成";

    std::cin.get();

}

 

3.二进制内存文件拷贝

#include<iostream>

#include <fstream>

#include <string>

 

using namespace std;

 

struct MyStruct

{

    int i;

    double db;

};

 

void main()

{

    ofstream fout("E:\\big.txt", ios::binary);

    MyStruct ss[5] = { { 1, 1.1 }, { 2, 2.2 }, { 3, 3.3 }, { 4, 4.4 }, {5, 5.5 } };

    fout.write((char *)ss, sizeof(MyStruct)* 5);

    fout.close();

 

    ifstream fin("E:\\big.txt", ios::binary);

    MyStruct *p = new MyStruct[5];//动态分配内存

    fin.read((char *)p, sizeof(MyStruct)* 5);

    fin.close();

 

    for (int i = 0; i < 5; i++)

    {

        std::cout << p[i].i << " " << p[i].db << std::endl;

    }

 

    cin.get();

}

运行结果:

3.随机二进制文件读写

#include<iostream>

#include <fstream>

using namespace std;

 

void main1a()

{

    double db[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9,10.1 };

    ofstream  fout("N.txt", ios::binary);

    fout.write((char *)db, 80);

    fout.close();

    double *p = new double[10];

    ifstream fin("N.txt", ios::binary);

 

    fin.read((char *)p, 80);

    fin.close();

    for (int i = 0; i < 10; i++)

    {

        std::cout << p[i] << endl;

    }

 

    std::cin.get();

}

 

void main2b()//随机位置读取

{

    double db[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9,10.1 };

    ofstream  fout("N.txt", ios::binary);

    fout.write((char *)db, 80);

    fout.close();

    double *p = new double[5];

    ifstream fin("N.txt", ios::binary);

    fin.seekg(-40, ios::end);//读写

    fin.read((char *)p, 40);

    fin.close();

    for (int i = 0; i < 5; i++)

    {

        std::cout << p[i] << endl;

    }

 

    std::cin.get();

}

 

 

void main()

{

    //先读取

    double *p = new double[10];

    ifstream fin("N.txt", ios::binary);

    fin.read((char *)p, 80);

    fin.close();

    for (int i = 0; i < 10; i++)

    {

        std::cout << p[i] << endl;

    }

    //随机写入

    double db[] = { 100.9, 200.8, 300.7, 400.6, 500.5 };

    //ios::in | ios::out不再清零文件,否则会清零

    ofstream  fout("N.txt", ios::binary | ios::in | ios::out);

    //fout.seekp(40, ios::beg);

    fout.seekp(0, ios::end);//写入

    fout.write((char *)db, 40);

    fout.close();

 

    //再次读取

    {

        double *p = new double[20];

        ifstream fin("N.txt", ios::binary);

        fin.read((char *)p, 160);

        fin.close();

        for (int i = 0; i < 20; i++)

        {

            std::cout << p[i] << endl;

        }

    }

 

    std::cin.get();

}

4.随机文件读写

#include<iostream>

#include <fstream>

using namespace std;

 

void main1()//随机位置读取

{

    ofstream fout("p.txt");

    fout << "1234567890abcdefghijklmn";

    fout.close();

    ifstream fin("p.txt");

    //fin.seekg(9, ios::beg);//从开始,往前9个字符

    fin.seekg(-9, ios::end);//从尾部,倒数9个字符

    char ch;

    while (fin.get(ch))

    {

        cout << ch;

    }

    fin.close();

    cin.get();

}

 

void main2()

{

    ofstream fout("px.txt");

    fout << "1234567890abcdefghijklmn";

    fout.close();

 

    ofstream Fout("px.txt", ios::in | ios::out);

    char str[] = "ABCDEFG";

    Fout.seekp(0, ios::end);//随机位置进行读写

    long size = Fout.tellp();//当前位置距离begin有多少个字节,尾部可以获取文件大小

 

    cout << size << endl;

    Fout << str;

    Fout.close();

 

    ifstream fin("px.txt");

    char ch;

    while (fin.get(ch))

    {

        cout << ch;

    }

 

    fin.close();

    cin.get();

}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

涂作权的博客

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

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

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

打赏作者

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

抵扣说明:

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

余额充值