vicg
vicg

Reputation: 1368

Trouble reading row of doubles from file C++

for some reason the code below stores a bunch of 0s into my doubles array and it doesn't write the 0s into the file im trying to create. This is my first time programming in c++ so I'm still getting used to some of the basic things. Any help is appreciated.

#include <iostream>
#include <fstream>
#include <random>

using namespace std;

double* getMatrix(int m, int n, char const*  fileName) {
    ifstream inFile(fileName);
    if(!inFile.is_open()) {
        throw std::runtime_error("failed to open file");
    }
    double* newMatrix = new double[m*n];
    for (int i = 0; i < m * n; ++i) {
        inFile >> newMatrix[i];
    }
    inFile.close();
    return newMatrix;
}

void writeMatrix(int n, int m, double* matrix, char const* fileName) {
    ofstream out(fileName);
    for(int i=0; i < m*n && out; ++i) {
        out << matrix[i] << "\n";
    }
    return;
}

int main(int argc, char *argv[]) {
    double* newMatrix = getMatrix(3, 4, "matrixA.txt");
    for(int i = 0; i < 12; ++i) {
        cout << newMatrix[i] << endl;
    }
    writeMatrix(3, 4, newMatrix, "matrixC.txt");
    delete newMatrix;
}

and here is the file I'm trying to read in

   0.314723686393179
   0.405791937075619
  -0.373013183706494
   0.413375856139019
   0.132359246225410
  -0.402459595000590
  -0.221501781132952
   0.046881519204984
   0.457506835434298
   0.464888535199277
  -0.342386918322452
   0.470592781760616

edit: updated getMatrix function to what is below and now I'm getting wrong values in output and still unable to create the file "matrixC.txt"

double* getMatrix(int m, int n, char* const fileName) {
    ifstream inFile(fileName);
    double* newMatrix = new double[m*n];
    try {
        for (int i = 0; i < m * n; ++i) {
            inFile >> newMatrix[i];
        }
    } catch (ifstream::failure e) {
        cout << "exception opening file";
    }

    inFile.close();
    return newMatrix;
}

and here is what I am getting as output

-1.28823e-231
-1.28823e-231
6.95324e-310
6.95327e-310
6.95324e-310
6.95327e-310
0
0
0
0
0
0

edit2: updated the main code block with newer code that handles if the file is not open. Still doesn't

edit3:
I solved the problem, I added this line set(CMAKE_RUNTIME_OUTPUT_DIRECTORY"~/ClionProjects/MatMult") to my CMakeLists.txt file

Upvotes: 0

Views: 320

Answers (1)

moooeeeep
moooeeeep

Reputation: 32522

I just tried to compile and run your code and after a quick fixing of include directives it compiled just fine and I could not reproduce your error.

Here's the code I used:

#include <iostream>
#include <fstream>
#include <stdexcept>

using namespace std;

double* getMatrix(int m, int n, char const*  fileName) {
    ifstream inFile(fileName);
    if(!inFile.is_open()) {
        throw std::runtime_error("failed to open file");
    }
    double* newMatrix = new double[m*n];
    for (int i = 0; i < m * n; ++i) {
        inFile >> newMatrix[i];
    }
    inFile.close();
    return newMatrix;
}

void writeMatrix(int n, int m, double* matrix, char const* fileName) {
    ofstream out(fileName);
    for(int i=0; i < m*n && out; ++i) {
        out << matrix[i] << "\n";
    }
    return;
}

int main(int argc, char *argv[]) {
    double* newMatrix = getMatrix(3, 4, "matrixA.txt");
    for(int i = 0; i < 12; ++i) {
        cout << newMatrix[i] << endl;
    }
    writeMatrix(3, 4, newMatrix, "matrixC.txt");
    delete newMatrix;
}

Here's how I built and ran:

$ g++ test.cc && ./a.out > test.txt
$ diff test.txt matrixC.txt
$ cat matrixC.txt 
0.314724
0.405792
-0.373013
0.413376
0.132359
-0.40246
-0.221502
0.0468815
0.457507
0.464889
-0.342387
0.470593

Isn't this how it's supposed to be?

Upvotes: 1

Related Questions