将C结果保存为二进制文件供以后使用
作者:互联网
我正在编写一个C程序来解决PDE并将解决方案保存在2D阵列中.然后它会对此PDE的结果执行某些操作.然而,准确地解决PDE需要大量的计算.因此,解决部分只需要执行一次,但结果应该保存,以便我以后可以回复它们.
因此,我写了两个不同的代码:
>解决pde并将结果保存在矩阵A中.矩阵A保存在二进制文件中.
>读取二进制文件中的值并对结果进行计算.
但是,当我在第二个代码中读取二进制文件时,它只是一个空数组,并且不包含我在其他代码中保存的值.
如果我在代码1中读取它,它会按照需要运行.
我究竟做错了什么?甚至可以在其他应用程序中读取二进制文件的值吗?我应该完全解决这个问题吗?
如果我以某种方式链接它们,我将这两个代码保存在一个不同的项目中?或者两个代码是否应该在同一个项目中?
我花了几天时间在网上搜索解决方案,但我只找到有关如何在同一代码中打开和保存二进制文件的问题和答案(例如this和this).这是否意味着我想要做的事情是不可能的,或者我找不到正确的来源?
我是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
解决方法:
一个主要问题是:
out.write((char *) &A, sizeof A);
在这里,您可以写出A的位置,而不是它指向的数据.此外,由于A是指针,因此A的大小将是指针的大小而不是指向的大小.
您需要显式循环并保存A的每个子数组:
for (int i = 0; i < ssteps; ++i)
out.write((char *) A[i], tsteps * sizeof(double));
阅读时反其道而行之.
标签:c,binaryfiles 来源: https://codeday.me/bug/20190724/1521660.html