提示:文章
文章目录
前言
前期疑问:
本文目标:
一、背景
这个文章主要是针对二进制文件的读写
大概会分为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
总结
未完待续