二进制读写文件

提示:文章

文章目录

前言

前期疑问:

本文目标:


一、背景

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

大概会分为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

总结

未完待续

相关推荐
数据小爬虫@16 分钟前
如何高效利用Python爬虫按关键字搜索苏宁商品
开发语言·爬虫·python
ZJ_.17 分钟前
WPSJS:让 WPS 办公与 JavaScript 完美联动
开发语言·前端·javascript·vscode·ecmascript·wps
Narutolxy23 分钟前
深入探讨 Go 中的高级表单验证与翻译:Gin 与 Validator 的实践之道20241223
开发语言·golang·gin
Hello.Reader30 分钟前
全面解析 Golang Gin 框架
开发语言·golang·gin
禁默41 分钟前
深入浅出:AWT的基本组件及其应用
java·开发语言·界面编程
Code哈哈笑1 小时前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
程序猿进阶1 小时前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot
qq_433618441 小时前
shell 编程(二)
开发语言·bash·shell
charlie1145141911 小时前
C++ STL CookBook
开发语言·c++·stl·c++20
袁袁袁袁满1 小时前
100天精通Python(爬虫篇)——第113天:‌爬虫基础模块之urllib详细教程大全
开发语言·爬虫·python·网络爬虫·爬虫实战·urllib·urllib模块教程