我正在编写一个C++程序,它解决了一个PDE并将解决方案保存在一个2D数组中。然后用这个PDE的结果做一些事情。然而,精确地求解PDE需要大量的计算。因此,解决部分只能执行一次,但结果应该保存,这样我可以稍后再回到它们。
因此,我写了两个不同的代码:
但是,当我在第二段代码中读取二进制文件时,它只是一个空数组,不包含我在其他代码中保存的值。
如果我在代码1中读到它,它就会按需要工作。
我做错了什么?甚至可以在其他应用程序中读取二进制文件的值吗?我应该完全不同地处理这个问题吗?
我在一个不同的项目中保存了两个代码,我应该以某种方式将它们链接起来吗?还是这两个代码应该在同一个项目中?
我花了几天时间在网络上寻找解决方案,但我只找到了关于如何在相同的代码中打开和保存二进制文件(例如这和这)的问题和答案。这是否意味着我试图做的事情是不可能的,还是我没有找到正确的来源?
我是一个完全的初学者在C++,并将非常高兴的任何帮助或指导。
事先非常感谢!
这是代码1:
#include <iostream>
using namespace std;
#include <math.h>
#include <getopt.h>
#include <fstream>
#include <string>
// Returns a pointer-to-pointer to a newly created array
// of size [row : col].
double **Create2D(int row, int col)
{
double **p = new double* [row];
for (int j = 0; j < row; j ++)
p[j] = new double[col];
return p;
}
// Deletes an array pointed by 'p' that has 'row' number rows
void Delete2D(double **p, int row)
{
for (int j = 0; j < row; j ++)
delete [] p[j];
delete [] p;
}
double PDE (double A_old, double A_old2, double dt, double s_int, double m, double ds, double R, double d){
double A_next;
A_next = A_old + dt*(s_int - 1)*m*A_old + (dt/ds)*((s_int-1)*(R*s_int-d)*(A_old2-A_old));
return A_next;
}
// Start of main function
int main(int argc, char *argv[]) {
// Set default values for input
double t(1), m(0.5) , R (1.3) , d (0.8) ;
// Solution by solving PDE for PGF
double dt = 0.0003;
const int tsteps = t/dt +1;
const double ads = 0.01;
const double ds = 0.01;
const int ssteps = 1/ds +1;
// Start Active Section
double **A = Create2D(ssteps, tsteps);
// Initial condition 1: f(s,0) = 1
int i = 0;
for (;i<ssteps;){
A[i][0] = 1;
i++;
}
// Initial condition 2: f(1,t) = 1
int j = 0;
for (;j<tsteps;){
A[ssteps-1][j] = 1;
j++;
}
// Calculate other matrix points
int ii = ssteps-2;
double as_int;
as_int = 1-ads;
for (;ii>=0;){
int jj = 0;
for (;jj<tsteps-1;){
A[ii][jj+1] = PDE (A[ii][jj], A[ii+1][jj], dt, as_int, m, ds, R, d);
jj++;
}
as_int = as_int-ds;
ii--;
}
// Write A to a binary document
ofstream out("valuesA", ios::out | ios::binary);
if(!out) {
cout << "Cannot open file.";
return 1;
}
out.write((char *) &A, sizeof A);
out.close();
ifstream in("valuesA", ios::in | ios::binary);
in.read((char *) &A, sizeof A);
// see how many bytes have been read
cout << in.gcount() << " bytes read\n";
in.close();
// Delete A from the heap to prevent memory leaking
Delete2D(A, ssteps);
return 0;
} // end main这是代码2:
#include <iostream>
using namespace std;
#include <math.h>
#include <getopt.h>
#include <fstream>
#include <string>
double **Create2D(int row, int col)
{
double **p = new double* [row];
for (int j = 0; j < row; j ++)
p[j] = new double[col];
return p;
}
// Start of main function
int main(int argc, char *argv[]) {
// Use output of PDE function
double dt = 0.0003;
const int tsteps = t/dt +1;
const double ds = 0.01;
const int ssteps = 1/ds +1;
double **A = Create2D(ssteps, tsteps);
ifstream in("valuesA", ios::in | ios::binary);
in.read((char *) &A, sizeof A);
// see how many bytes have been read
cout << in.gcount() << " bytes read\n";
in.close();
return 0;
} // end main发布于 2018-04-12 11:55:41
一个主要问题是:
out.write((char *) &A, sizeof A);这里写的是A的位置,而不是它指向的数据。此外,由于A是一个指针,那么sizeof A将是指针的大小,而不是它所指向的。
您需要显式地循环和保存A的每个子数组:
for (int i = 0; i < ssteps; ++i)
out.write((char *) A[i], tsteps * sizeof(double));阅读时要做相反的事情。
发布于 2018-04-12 14:22:20
我认为@SomeProgrammerDude解决了您的问题,但我建议您看看boost序列化。
因此,您可以轻松地写入和读取完整对象的状态,而不必为数据创建自己的二进制表示。
https://stackoverflow.com/questions/49795772
复制相似问题