简体   繁体   中英

Reading/Writing to a binary file in C++

I have a large matrix of values that I am trying to store in external memory.

I am writing the matrix to a file and then editing the file using read/write commands one row at a time. Originally, I was reading the matrix from the file using the getLine() method and that was too inefficient in terms of time.

Now, I am trying to write to a binary file using the fstream class read/write commands. My program terminates when it reaches the line:

IOfile.read(reinterpret_cast<char*>(&d1), (n - i) * sizeof(double)); //gets all elements in the row after element i

Why is it not reading the values from the file into vector 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;
}

Why is it able to write to the file in binary but not read from it? Any help is appreciated, thanks.

Try

IOfile.read(reinterpret_cast<char*>(d1.data()), (n - i) * sizeof(double));

The address of the vector is different than the underlying array you're trying to write to. This also applies to d2 etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM