C++ 读取/写入C+中的二进制文件+;
我有一个很大的值矩阵,我正试图存储在外部内存中 我将矩阵写入文件,然后使用读/写命令一次编辑一行。最初,我使用getLine()方法从文件中读取矩阵,这在时间上效率太低 现在,我正在尝试使用fstream类读/写命令写入二进制文件。我的程序到达以下行时终止:C++ 读取/写入C+中的二进制文件+;,c++,C++,我有一个很大的值矩阵,我正试图存储在外部内存中 我将矩阵写入文件,然后使用读/写命令一次编辑一行。最初,我使用getLine()方法从文件中读取矩阵,这在时间上效率太低 现在,我正在尝试使用fstream类读/写命令写入二进制文件。我的程序到达以下行时终止: IOfile.read(reinterpret_cast<char*>(&d1), (n - i) * sizeof(double)); //gets all elements in the row after elem
IOfile.read(reinterpret_cast<char*>(&d1), (n - i) * sizeof(double)); //gets all elements in the row after element i
IOfile.read(reinterpret_cast(&d1),(n-i)*sizeof(double))//获取元素i之后的行中的所有元素
为什么不将文件中的值读入向量d1
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
vector<double> vector_solve(vector <double>& B) {
vector <double> MI(B.size());
int n = B.size();
/* Writing a matrix of values to the file */
fstream IOfile;
IOfile.open("test_matrix.bin", ios::in | ios::out | ios::binary);
if (!IOfile.is_open())
cout << "Error opening file: test_matrix.bin" << endl;
else {
for (int i = 0; i < n; i++) //rows
for (int j = 0; j <= n; j++) //columns
if (j == n) //write final column as original vector
IOfile.write(reinterpret_cast<char*>(&B[i]), sizeof(double));
else {
double d1 = 500; //TO-DO: double d1 = (*Mfunc)(i, j, dtMU);
IOfile.write(reinterpret_cast<char*>(&d1), sizeof(double));
}
//loop to perform the gauss elimination
for (int i = 0; i < (n - 1); i++) {
for (int k = (i + 1); k < n; k++) {
vector <double> d1(n + 1); //row1
vector <double> d2(n + 1); //row2
int SG1 = i * sizeof(double); //file get pointer
cout << "SG1 is: " << SG1 << endl;
IOfile.seekg(SG1); //Sets the get position to element i
cout << "ABOUT TO READ" << endl;
IOfile.read(reinterpret_cast<char*>(&d1), (n - i) * sizeof(double)); //gets all elements in the row after element i
cout << "d1[0] is equal to: " << d1[0] << endl;
cout << "d1[1] is equal to: " << d1[1] << endl;
cout << "d1[2] is equal to: " << d1[2] << endl;
cout << "d1[3] is equal to: " << d1[3] << endl;
int SG2 = k * sizeof(double);
IOfile.seekg(SG2); //Sets the get position to element k
IOfile.read(reinterpret_cast<char*>(&d2), (n - k) * sizeof(double)); //gets all elements in the row after element k
double t = d2[i] / d1[i];
for (int j = 0; j <= n; j++) {
double temp1 = d1[j];
d2[j] = d2[j] - t * temp1;
}
IOfile.seekp(SG2);
IOfile.write(reinterpret_cast<char*>(&d2), sizeof(d2));
}
for (int i = (n - 1); i >= 0; i--) {
vector <double> d1(n + 1);
int SG1 = i * sizeof(double);
IOfile.seekg(SG1);
IOfile.read(reinterpret_cast<char*>(&d1), (n - i) * sizeof(double));
MI[i] = d1[n];
for (int j = (i + 1); j < n; j++)
if (j != i)
MI[i] = MI[i] - d1[j] * MI[j];
MI[i] = MI[i] / d1[i];
}
IOfile.close();
}
}
return MI;
}
#包括
#包括
#包括
使用名称空间std;
向量求解(向量与B){
向量MI(B.size());
int n=B.size();
/*将值矩阵写入文件*/
fstream文件;
open(“test_matrix.bin”,ios::in | ios::out | ios::binary);
如果(!IOfile.is_open())
试试看
IOfile.read(reinterpret_cast(d1.data()),(n-i)*sizeof(double));
向量的地址与您试图写入的底层数组不同。这也适用于d2等。代码有点大,所以我还没有全部阅读。我假设d1的大小合适,如果是,那么您将传递d1.data()
。选择唯一的名称。d1首先被视为双代号,这会让一些人认为您在编写代码方面非常糟糕。感谢您花时间。d1足够大,可以容纳整个矩阵行。很遗憾,我无法调用d1.data()由于程序在执行读取功能时终止,因此它永远不会到达打印语句:如果写入和读取是分开进行的,为什么不关闭并重新打开文件?您可以根据需要重新使用此代码:给定的d1
是一个向量,d1.size()
优于n-1
。
IOfile.read(reinterpret_cast<char*>(d1.data()), (n - i) * sizeof(double));