gpt4 book ai didi

c - 将数组写入二进制文件?

转载 作者:太空狗 更新时间:2023-10-29 14:55:01 46 4
gpt4 key购买 nike

我需要一些帮助——下一段代码将一个长 double 动态数组写入文件

int nx = 10, ny = 10;
long double **data = new long double *[nx];
long double **data_read = new long double *[nx];
for (int i = 0; i < nx; i++) {
data[i] = new long double [ny];
data_read[i] = new long double [ny];
}

data[4][4] = 10.0;
printf("%LF\n", data[4][4]);

FILE *file = fopen("data", "wb");
fwrite(data, nx * ny * sizeof(data), 1, file);
fclose(file);

file = fopen("data", "rb");
fread(data, nx * ny * sizeof(data_read), 1, file );
fclose(file);

printf("%LF\n", data_read[4][4]);

但是data[4][4] != data_read[4][4],因为从文件读取后data_read[4][4]=0.0

有人知道我做错了什么吗?

最佳答案

您需要单独写入指针数组中的每一行。批量写入不适用于伪造的二维数组(或 nD)的指针到指针实现。

对于写作:

for (int i=0; i<nx; ++i)
fwrite(data[i], sizeof(data[i][0]), ny, file);

阅读:

for (int i=0; i<nx; ++i)
fread(data[i], sizeof(data[i][0]), ny, file);

坦率地说,您很(不)幸运该进程没有彻底崩溃,因为您正在将一堆内存地址写入磁盘文件(十六进制转储会向您显示),并且很可能会离开在两个 操作期间结束您的指针数组分配。

也就是说,我会开始学习标准 C++ IO 库,而不是在 C++ 世界中使用 C 代码(或修复此问题上的标签)。


单 block 写入/读取

您询问是否可以将其作为单个 block 读/写来执行。答案是肯定的,但是必须连续分配内存。如果你仍然想要一个指针到指针数组,你当然可以使用一个。虽然我建议使用 std::vector<long double>对于数据缓冲区,下面将演示我所指的内容:

int main()
{
int nx = 10, ny = 10;

long double *buff1 = new long double[nx * ny];
long double *buff2 = new long double[nx * ny];

long double **data = new long double *[nx];
long double **data_read = new long double *[nx];

for (int i = 0; i < nx; i++)
{
data[i] = buff1 + (i*ny);
data_read[i] = buff2 + (i*ny);
}

data[4][4] = 10.0;
printf("%LF\n", data[4][4]);

FILE *file = fopen("data.bin", "wb");
fwrite(buff1, sizeof(*buff1), nx * ny, file);
fclose(file);

file = fopen("data.bin", "rb");
fread(buff2, sizeof(*buff2), nx * ny, file );
fclose(file);

printf("%LF\n", data_read[4][4]);

// delete pointer arrays
delete [] data;
delete [] data_read;

// delete buffers
delete [] buff1;
delete [] buff2;
}

输出

10.000000
10.000000

使用 std::vector<>对于 RAII解决方案

所有这些分配都会变得困惑,坦率地说容易出现问题。考虑一下这有何不同:

#include <iostream>
#include <fstream>
#include <vector>

int main()
{
int nx = 10, ny = 10;

// buffers for allocation
std::vector<long double> buff1(nx*ny);
std::vector<long double> buff2(nx*ny);

// holds pointers into original
std::vector<long double*> data(nx);
std::vector<long double*> data_read(nx);

for (int i = 0; i < nx; i++)
{
data[i] = buff1.data() + (i*ny);
data_read[i] = buff2.data() + (i*ny);
}

data[4][4] = 10.0;
std::cout << data[4][4] << std::endl;

std::ofstream ofp("data.bin", std::ios::out | std::ios::binary);
ofp.write(reinterpret_cast<const char*>(buff1.data()), buff1.size() * sizeof(buff1[0]));
ofp.close();

std::ifstream ifp("data.bin", std::ios::in | std::ios::binary);
ifp.read(reinterpret_cast<char*>(buff2.data()), buff2.size() * sizeof(buff2[0]));
ifp.close();

std::cout << data_read[4][4] << std::endl;

return 0;
}

关于c - 将数组写入二进制文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21213543/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com