二进制读写文件

提示:文章

文章目录

前言

前期疑问:

本文目标:


一、背景

这个文章主要是针对二进制文件的读写

大概会分为c语言对二进制文件读写和c++对二进制文件的读写

查找资料看到这篇文章:二进制文件的读写操作,文章是分别对整型变量、数组、字符串进行读写。我其实想得到是对混合的数据的读写,但是我突然想到可以将整型数据转成char型然后使用字符串写成二进制啊。没毛病,那么很长的字符串(大于1024)是怎么写入呢。可以试一下。

二、c语言读写二进制文件

2.1

二进制读写文件

cpp 复制代码
#include <iostream>
#include <stdio.h>
#include <string.h>

struct BinaryWriteStruct
{
    char a;
    int b;
    float c;
    double d;
    char array[15];
    //std::string e;
    BinaryWriteStruct()
    {
        a = '0';
        b = 0;
        c = 0.0f;
        d = 0;
        memset(array, 0, sizeof(array));
    }
};

typedef BinaryWriteStruct BinaryWrite;

struct BinaryWriteStruct2
{
    char a;
    int b;
    float c;
    double d;
    char array[15];
};

typedef BinaryWriteStruct2 BinaryWrite2;

void BinaryWriteInteger()
{
    FILE* writeFile = fopen("E:\\writeInteger.txt", "wb");
    if (writeFile == nullptr) {
        printf("%s", "file open failed\n");
    }
    // 存储一个整型变量
    int a = -11; 	//-1的二进制:

    fwrite(&a, sizeof(int), 1, writeFile);
    fclose(writeFile);
}

void BinaryReadInteger()
{
    FILE* readFile = fopen("E:\\writeInteger.txt", "r");
    if (readFile == nullptr) {
        printf("%s", "file open failed\n");
    }
    // 存储一个整型变量
    int a = 0; 	//-1的二进制:

    fread(&a, sizeof(int), 1, readFile);
    printf("a:%d\n", a);
    fclose(readFile);
}

void BinaryWriteStruct()
{
    FILE* writeFile = fopen("E:\\writeStruct.txt", "wb");
    if (writeFile == nullptr) {
        printf("%s", "file open failed\n");
    }
    // 存储一个结构体变量
    BinaryWrite binaryWrite;
    
    binaryWrite.a = 'Q';
    binaryWrite.b = 1;
    binaryWrite.c = 2.1f;
    binaryWrite.d = 4;
    strcpy(binaryWrite.array, "hello world");
    //binaryWrite.e = "this ia a string";       //这边写入string会导致读不到正确的数据,fwrite是c函数,std::string是c++对象

    // 测试结构体,验证为什么BinaryWrite结构体不能定义时直接赋值初始化
    BinaryWrite2 binaryWrite2 = {'Q', 1, 2, 3, "test"};        //当结构体没有构造函数时可以这样初始化,有构造函数就要上述初始化方式

    fwrite(&binaryWrite, sizeof(BinaryWrite), 1, writeFile);
    fclose(writeFile);
}

void BinaryReadStruct()
{
    FILE* readFile = fopen("E:\\writeStruct.txt", "rb");            //这边不写"rb"会读不到数据
    if (readFile == nullptr) {
        printf("%s", "file open failed\n");
    }
    // 存储一个整型变量
    BinaryWrite binaryWrite;

    fread(&binaryWrite, sizeof(BinaryWrite), 1, readFile);
    printf("a:%c\n", binaryWrite.a);
    printf("b:%d\n", binaryWrite.b);
    printf("c:%f\n", binaryWrite.c);
    printf("d:%f\n", binaryWrite.d);
    printf("array:%s\n", binaryWrite.array);
    //printf("e:%s\n", binaryWrite.e.c_str());
    fclose(readFile);
}

void BinaryWriteString()
{
    char array[] = {
            "data:34567668-0-05678709886r434563212212225113e65r41325541235412344236544265344652344542365442356442653446"
            "5234465234464236544235644524423445243442344244234452344523442534452434524345243452434562434562344524345245"
            "454564544,data:34567668-0-05678709886r434563212212225113e65r4132554123541234423654426534465234454236544235"
            "6442653446523446523446423654423564452442344524344234424423445234452344253445243452434524345243456243456234"
            "4524345245454564544,data:34567668-0-05678709886r434563212212225113e65r413255412354123442365442653446523445"
            "4236544235644265344652344652344642365442356445244234452434423442442344523445234425344524345243452434524345"
            "62434562344524345245454564544,data:34567668-0-05678709886r434563212212225113e65r41325541235412344236544265"
            "3446523445423654423564426534465234465234464236544235644524423445243442344244234452344523442534452434524345"
            "243452434562434562344524345245454564544,data:34567668-0-05678709886r434563212212225113e65r4132554123541234"
            "4236544265344652344542365442356442653446523446523446423654423564452442344524344234424423445234452344253445"
            "2434524345243452434562434562344524345245454564544, end"};
    FILE* writeFile = fopen("E:\\writeString.nag", "wb");
    if (writeFile == nullptr) {
        printf("%s", "file open failed\n");
    }

    fwrite(array, sizeof(char), sizeof(array), writeFile);
    fclose(writeFile);
}
void BinaryReadString()
{
    FILE* readFile = fopen("E:\\writeString.txt", "rb");
    if (readFile == nullptr) {
        printf("%s", "file open failed\n");
    }
    char array[10000] = {'\0'};

    char tempStr[100] = {'\0'};
    int count = 0;
    while (!(feof(readFile) || ferror(readFile))) {
        fread(tempStr, sizeof(char), sizeof(tempStr), readFile);
        strcpy(array + 100 * count, tempStr);
        count++;
    }
    if (ferror(readFile)) {
        printf("file read fail\n");
    }
    fclose(readFile);
    printf("%s\n", array);
}

int main()
{
    // 二进制写入整型
    BinaryWriteInteger();
    BinaryReadInteger();
    // 二进制写入结构体
    BinaryWriteStruct();
    BinaryReadStruct();
    // 二进制写入字符串
    BinaryWriteString();
    BinaryReadString();
    return 0;
}

打印结果

shell 复制代码
a:-11
a:Q
b:1
c:2.100000
d:4.000000
array:hello world
data:34567668-0-05678709886r434563212212225113e65r4132554123541234423654426534465234454236544235644265344652344652344642
36544235644524423445243442344244234452344523442534452434524345243452434562434562344524345245454564544,data:34567668-0-05
678709886r434563212212225113e65r4132554123541234423654426534465234454236544235644265344652344652344642365442356445244234
45243442344244234452344523442534452434524345243452434562434562344524345245454564544,data:34567668-0-05678709886r43456321
2212225113e65r4132554123541234423654426534465234454236544235644265344652344652344642365442356445244234452434423442442344
52344523442534452434524345243452434562434562344524345245454564544,data:34567668-0-05678709886r434563212212225113e65r4132
554123541234423654426534465234454236544235644265344652344652344642365442356445244234452434423442442344523445234425344524
34524345243452434562434562344524345245454564544,data:34567668-0-05678709886r434563212212225113e65r4132554123541234423654
426534465234454236544235644265344652344652344642365442356445244234452434423442442344523445234425344524345243452434524345
62434562344524345245454564544, end

三、c++读写二进制文件

3.1

下面是c++二进制读写文件

cpp 复制代码
void BinaryWriteStringByCpp()
{
    char array[] = {
            "data:34567668-0-05678709886r434563212212225113e65r41325541235412344236544265344652344542365442356442653446"
            "5234465234464236544235644524423445243442344244234452344523442534452434524345243452434562434562344524345245"
            "454564544,data:34567668-0-05678709886r434563212212225113e65r4132554123541234423654426534465234454236544235"
            "6442653446523446523446423654423564452442344524344234424423445234452344253445243452434524345243456243456234"
            "4524345245454564544,data:34567668-0-05678709886r434563212212225113e65r413255412354123442365442653446523445"
            "4236544235644265344652344652344642365442356445244234452434423442442344523445234425344524345243452434524345"
            "62434562344524345245454564544,data:34567668-0-05678709886r434563212212225113e65r41325541235412344236544265"
            "3446523445423654423564426534465234465234464236544235644524423445243442344244234452344523442534452434524345"
            "243452434562434562344524345245454564544,data:34567668-0-05678709886r434563212212225113e65r4132554123541234"
            "4236544265344652344542365442356442653446523446523446423654423564452442344524344234424423445234452344253445"
            "2434524345243452434562434562344524345245454564544, cpp end"};
    std::ofstream outputStream("E:\\writeStringByCpp.nag", std::ios::out | std::ios::binary);
    if (!outputStream.is_open()) {
        printf("file open failed\n");
    }
    outputStream.write(array, sizeof(array));
}

void BinaryReadStringByCpp()
{
    std::ifstream inputStream("E:\\writeStringByCpp.nag", std::ios::in | std::ios::binary);
    if (!inputStream.is_open()) {
        printf("file open failed\n");
    }
    std::stringstream inputStr;
    inputStr << inputStream.rdbuf();
    std::string str(inputStr.str());
    printf("%s\n", str.c_str());
}

int main()
{
    // c++二进制读写字符串
    BinaryWriteStringByCpp();
    BinaryReadStringByCpp();
    return 0;
}

打印信息

shell 复制代码
data:34567668-0-05678709886r434563212212225113e65r4132554123541234423654426534465234454236544235644265344652344652344642
36544235644524423445243442344244234452344523442534452434524345243452434562434562344524345245454564544,data:34567668-0-05
678709886r434563212212225113e65r4132554123541234423654426534465234454236544235644265344652344652344642365442356445244234
45243442344244234452344523442534452434524345243452434562434562344524345245454564544,data:34567668-0-05678709886r43456321
2212225113e65r4132554123541234423654426534465234454236544235644265344652344652344642365442356445244234452434423442442344
52344523442534452434524345243452434562434562344524345245454564544,data:34567668-0-05678709886r434563212212225113e65r4132
554123541234423654426534465234454236544235644265344652344652344642365442356445244234452434423442442344523445234425344524
34524345243452434562434562344524345245454564544,data:34567668-0-05678709886r434563212212225113e65r4132554123541234423654
426534465234454236544235644265344652344652344642365442356445244234452434423442442344523445234425344524345243452434524345
62434562344524345245454564544, cpp end

总结

未完待续

相关推荐
Deryck_德瑞克4 分钟前
Java集合笔记
java·开发语言·笔记
MengYiKeNan9 分钟前
C++二分函数lower_bound和upper_bound的用法
开发语言·c++·算法
会发paper的学渣14 分钟前
python 单例模式实现
开发语言·python·单例模式
学步_技术22 分钟前
Python编码系列—Python桥接模式:连接抽象与实现的桥梁
开发语言·python·桥接模式
柴华松25 分钟前
GPU训练代码
开发语言·python
好兄弟给我起把狙31 分钟前
[Golang] Select
开发语言·后端·golang
Echo_Lee033 分钟前
C#与Python脚本使用共享内存通信
开发语言·python·c#
python之行43 分钟前
python 环境问题
开发语言·python
小林熬夜学编程1 小时前
C++第五十一弹---IO流实战:高效文件读写与格式化输出
c语言·开发语言·c++·算法
不是编程家1 小时前
C++ 第三讲:内存管理
java·开发语言·c++