写数组的二进制文件? [英] Write array into the binary file?
问题描述
我需要一些帮助 - 下一块code写的长双动态数组到文件
I need some help - next piece of code writes a long double dynamic array into the file
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]);
但数据[4] [4]!= DATA_READ [4] [4]
,因为从文件中读取后 DATA_READ [4] [ 4] = 0.0
。
But data[4][4] != data_read[4][4]
, because after reading from file data_read[4][4]=0.0
.
任何人知道我在做什么错了?
Anybody knows what am I doing wrong?
推荐答案
您需要编写每一行的指针数组分别。一团写不会为假的二维数组(或Nd)的指针到指针的实施工作。
You need to write each row in your pointer array individually. A mass write will not work for pointer-to-pointer implementations of a fake 2D array (or 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- code(或修复在这个问题上的标签)。
That said, I'd start learning about the standard C++ IO library rather than using C-code in a C++ world (or fix the tag on this question).
单块写/读
您询问是否可以这样做,因为读/写单块。答案是肯定的,但你必须连续分配内存。如果你仍然想一个指针到指针数组,你当然可以使用一个。虽然我推荐使用的std ::矢量&lt;长双&GT;
数据缓冲区,下面就演示一下我指的是:
You asked if it is possible to do this as a single block read/write. The answer is yes, but you must allocate the memory contiguously. If you still want a pointer-to-pointer array you can certainly use one. Though I recommend using std::vector<long double>
for the data buffer, the following will demonstrate what I refer to:
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 ::矢量&lt;&GT;
为 RAII 解决方案
Using a std::vector<>
for an RAII Solution
所有这些分配会导致混乱,并坦言容易出现问题。考虑一下,这是不同的:
All those allocations can get messy, and frankly prone to problems. Consider how this is different:
#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;
}
这篇关于写数组的二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!